{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "604eecd8",
   "metadata": {},
   "source": [
    "# Introduction\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a768b523",
   "metadata": {},
   "source": [
    "## magic command in IPython and Jupyter Notebook\n",
    "1. %pwd\n",
    "2. ? ?? **?\n",
    "3. %paste %cpaste\n",
    "4. %run %load - 将其他脚本独立执行在当前环境中，但是能够访问其中的变量和计算结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "465e6778",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/Users/collinsliu/jupyter/python_learning_basics'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%pwd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9323977f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'{\\n'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "path = '/Users/collinsliu/jupyter/python_learning_basics/python_for_data_analysis_01.ipynb'\n",
    "open(path).readline()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "858babeb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['/Users/collinsliu/opt/anaconda3/lib/python3.9/site-packages/ipykernel_launcher.py', '-f', '/Users/collinsliu/Library/Jupyter/runtime/kernel-1cce7e54-f2d1-4678-8740-141e606afb0d.json']\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "\n",
    "print(sys.argv)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eeee5531",
   "metadata": {},
   "source": [
    "### time timeit\n",
    "1. time 计算一次运行的时间\n",
    "2. timeit 计算多次运行后的平均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c38a6635",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "120 µs ± 33.2 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.random.randn(100,100)\n",
    "%timeit np.dot(a,a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ef074dcf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 1.24 ms, sys: 605 µs, total: 1.85 ms\n",
      "Wall time: 328 µs\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[  1.50037706,  22.49623362, -12.12656798, ...,   1.72503629,\n",
       "         -5.24800675, -11.83171313],\n",
       "       [-11.38453896,   9.855974  ,   5.97792139, ...,  -4.89087597,\n",
       "         -2.01550345, -10.62897141],\n",
       "       [ -7.92667631,   8.35180731,  11.06144292, ...,  -7.81310707,\n",
       "         -5.49945916,  -1.7164878 ],\n",
       "       ...,\n",
       "       [ -1.25936348,  -9.7170556 , -18.96496902, ...,  -4.59154916,\n",
       "          4.18318841,  -6.05732026],\n",
       "       [ -6.83046293,   2.762347  ,  19.25781217, ...,  11.07428804,\n",
       "         -9.05828889,   0.78988761],\n",
       "       [  9.82414804,  -6.4448292 ,   0.87163114, ..., -10.98951163,\n",
       "        -17.29293344, -16.65044285]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%time np.dot(a,a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82e90eef",
   "metadata": {},
   "source": [
    "### who who_ls whos\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c324140f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%who_ls a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08accb44",
   "metadata": {},
   "source": [
    "### matplotlib | matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c79b6749",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "432f9e92",
   "metadata": {},
   "source": [
    "## python basic grammars"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18a62edf",
   "metadata": {},
   "source": [
    "### object basics\n",
    "1. built-in function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "94d314cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "######\n",
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# decide whether the object is of one kind - isinstance\n",
    "instance_a = 5\n",
    "print(isinstance(instance_a, int))\n",
    "print(\"######\")\n",
    "instance_b = \"name\"\n",
    "instance_c = 5.5\n",
    "print(isinstance(instance_b,str))\n",
    "print(isinstance(instance_c,(int, float)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8f7ef30f",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'isiterable' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m/var/folders/zh/fmxxp3g1653ccyjmyp5z3bc00000gn/T/ipykernel_84869/2577812497.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# decide whether the object has __iter__ function - which means it's iterrable\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0minstance_d\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"a string\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0misiterable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minstance_d\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'isiterable' is not defined"
     ]
    }
   ],
   "source": [
    "# decide whether the object has __iter__ function - which means it's iterrable\n",
    "instance_d = \"a string\"\n",
    "isiterable(instance_d)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b945f78",
   "metadata": {},
   "source": [
    "### formatting print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "136b7dfd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.50 is float\n"
     ]
    }
   ],
   "source": [
    "# print formatted results\n",
    "print(\"{:.2f} is float\".format(instance_c))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55fe8b12",
   "metadata": {},
   "source": [
    "### time in python\n",
    "1. datetime package\n",
    "2. two important function - **strftime** | **strptime**\n",
    "3. **strftime** 从已有的datetim对象转换为格式输出\n",
    "4. **strptime** 重新构造一个datetime对象并转换为格式输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "8e753e5e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19\n",
      "2022-11-09 Nov:11:1667994410\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "time data '20221109' does not match format '%Y-%m-%d'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m/var/folders/zh/fmxxp3g1653ccyjmyp5z3bc00000gn/T/ipykernel_84869/762992116.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhour\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstrftime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"%Y-%m-%d %h:%m:%s\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdatetime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstrptime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"20221109\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"%Y-%m-%d\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m~/opt/anaconda3/lib/python3.9/_strptime.py\u001b[0m in \u001b[0;36m_strptime_datetime\u001b[0;34m(cls, data_string, format)\u001b[0m\n\u001b[1;32m    566\u001b[0m     \"\"\"Return a class cls instance based on the input string and the\n\u001b[1;32m    567\u001b[0m     format string.\"\"\"\n\u001b[0;32m--> 568\u001b[0;31m     \u001b[0mtt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfraction\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgmtoff_fraction\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_strptime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata_string\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mformat\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    569\u001b[0m     \u001b[0mtzname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgmtoff\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    570\u001b[0m     \u001b[0margs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m6\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mfraction\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/opt/anaconda3/lib/python3.9/_strptime.py\u001b[0m in \u001b[0;36m_strptime\u001b[0;34m(data_string, format)\u001b[0m\n\u001b[1;32m    347\u001b[0m     \u001b[0mfound\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mformat_regex\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmatch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata_string\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    348\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mfound\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 349\u001b[0;31m         raise ValueError(\"time data %r does not match format %r\" %\n\u001b[0m\u001b[1;32m    350\u001b[0m                          (data_string, format))\n\u001b[1;32m    351\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata_string\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0mfound\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mValueError\u001b[0m: time data '20221109' does not match format '%Y-%m-%d'"
     ]
    }
   ],
   "source": [
    "from datetime import date, time, datetime\n",
    "\n",
    "dt = datetime(2022,11,9,19,46,50)\n",
    "print(dt.hour)\n",
    "print(dt.strftime(\"%Y-%m-%d %h:%m:%s\"))\n",
    "print(datetime.strptime(\"20221109\",\"%Y-%m-%d\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba6bc175",
   "metadata": {},
   "source": [
    "## innter structure"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0773f475",
   "metadata": {},
   "source": [
    "### immutable data structure - tuple\n",
    "1. tuple() - 将其他格式转换为tuple格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "22388cae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a=1, b=2, c=3\n",
      "a=4, b=5, c=6\n",
      "a=7, b=8, c=9\n"
     ]
    }
   ],
   "source": [
    "# tuple unpacking\n",
    "exp_list = [(1,2,3), (4,5,6), (7,8,9)]\n",
    "for a,b,c in exp_list:\n",
    "    print(\"a={0}, b={1}, c={2}\".format(a,b,c))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "419eb412",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3, 4, 5, 6, 7)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[3, 4, 5, 6, 7]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# tuple discarding\n",
    "exp_tup = 1,2,3,4,5,6,7\n",
    "print(exp_tup)\n",
    "a,b,*rest = exp_tup\n",
    "rest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "0c0e7acc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# tuple common method - built-in methods for tuple\n",
    "exp_tup2 = 1,2,3,4,54,3,3,3,3,3,3\n",
    "exp_tup2.count(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd3e4c74",
   "metadata": {},
   "source": [
    "### mutable. data structure - list\n",
    "1. list() - 将其他格式转换为list格式\n",
    "2. list经常用来实例化iterator或者是generator对象，直接使用**list()**函数即可\n",
    "3. list的内置函数： append(item), insert(pos,item), pop(pos), remove(item), extend(list) 时间上比“+”更加节省\n",
    "4. 判断元素是否在list中： 语法糖： in | not in - 但是是线性遍历，时间比dict或者是set要长很多\n",
    "5. list的高级函数： sort() - 直接在原list的基础上进行排序\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "0cd0ec25",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 5, 3.6, 'foo', [3, 5]]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# list could be modified in-place\n",
    "exp_list = [1, 3, 5, 3.6, \"foo\", [3,5]]\n",
    "exp_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "7e1e7f5e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 'this is changed', 5, 3.6, 'foo', [3, 5]]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "exp_list[1] = \"this is changed\"\n",
    "exp_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0931983",
   "metadata": {},
   "source": [
    "#### sort: in-place function\n",
    "1. has two parameters: key and reverse\n",
    "2. key 输入一个fun,这个函数的输入值就是list中的各个元素，返回值是需要排序的对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "ae9710be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3, 5], [6, 17], [2, 23], [7, 0], [12, 3], [42, 41], [45, 33], [87, 100]] <class 'list'>\n",
      "[[7, 0], [12, 3], [3, 5], [6, 17], [2, 23], [45, 33], [42, 41], [87, 100]] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "def sort_fun(lst):\n",
    "    '''\n",
    "    sort by second element in one input list\n",
    "    '''\n",
    "    return lst[1]\n",
    "\n",
    "exp_list1 = [[3,5],[6,17],[2,23],[7,0],[12,3],[42,41],[45,33],[87,100]]\n",
    "print(exp_list1, type(exp_list1))\n",
    "exp_list1.sort(key=sort_fun)\n",
    "print(exp_list1, type(exp_list1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6996fe0a",
   "metadata": {},
   "source": [
    "#### bisect module\n",
    "1. 是一个模块，使用二分法像一个list中插入数据\n",
    "2. 其中的bisect方法是找到应该插入的位置\n",
    "3. 其中的insort方法是将该值插入到相应的位置上"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "7318fb66",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[0, 3, 4, 5, 6, 7, 8, 12, 13, 15, 23, 34, 52, 60]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import bisect\n",
    "\n",
    "bisect_list = [13,23,6,4,3,60,7,15,34,52,5,0,12]\n",
    "bisect_list.sort()\n",
    "res = bisect.bisect(bisect_list,8)\n",
    "print(res)\n",
    "bisect.insort(bisect_list,8)\n",
    "bisect_list\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27f41c07",
   "metadata": {},
   "source": [
    "#### slicing\n",
    "1. 先开始的节点start是包含在区间中的，但是结束的节点end是不包含在区间中的 - 无论是从头到尾还是从尾到头都是一样\n",
    "2. 比如[:3]是0，1，2；[-6:-2]是-5，-4，-3，-2\n",
    "3. 无论是从前往后数还是从后往前数，开始的值都是0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33e02600",
   "metadata": {},
   "source": [
    "### built-in sequencing function\n",
    "1. enumerate\n",
    "2. sorted\n",
    "3. zip\n",
    "4. reversed"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6407b85f",
   "metadata": {},
   "source": [
    "#### enumerate - application: transfer list into dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "e966e27b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'foo': 0, 'bar': 1, 'tar': 2, 'war': 3, 'mar': 4}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# transfer list into dict\n",
    "exp_dict = {}\n",
    "exp_list = [\"foo\",'bar','tar','war','mar']\n",
    "for i,v in enumerate(exp_list):\n",
    "    exp_dict[v] = i\n",
    "exp_dict"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76649689",
   "metadata": {},
   "source": [
    "#### sorted - just like list built-in function sort()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d51fb09f",
   "metadata": {},
   "source": [
    "#### zip - packing differenct structures into one tuple\n",
    "1. 如果多个结构进行zip操作，最后结果长度将取决于最短的结构，多余的部分会被丢弃\n",
    "2. zip同时可以与\\* \\*\\* 同时使用达到unzip的效果\n",
    "3. zip的输出将会是一个zip对象，需要使用list()方法将这个对象实例化\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "9eee3fc3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0: ('foo', 'A'), 1: ('bar', 'B'), 2: ('tar', 'C'), 3: ('war', 'D')}\n",
      "<class 'zip'>\n",
      "<zip object at 0x7fb1a36ec640>\n",
      "[('foo', 'A'), ('bar', 'B'), ('tar', 'C'), ('war', 'D')]\n"
     ]
    }
   ],
   "source": [
    "# zip common usage example\n",
    "exp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\n",
    "exp_seq2 = (\"A\",\"B\",\"C\",\"D\")\n",
    "exp_dict2 = {}\n",
    "for i,v in enumerate(zip(exp_seq1, exp_seq2)):\n",
    "    exp_dict2[i] = v\n",
    "print(exp_dict2)\n",
    "print(zip(exp_seq1,exp_seq2))\n",
    "print(list(zip(exp_seq1,exp_seq2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "9f091e2a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Charlie', 'Lucy', 'Lisa', 'Peppermint', 'Marcy')"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# zip used for unzip\n",
    "# zip here just combine together all first names and last names\n",
    "coach = [(\"Charlie\",\"Brown\"),\n",
    "         (\"Lucy\",\"Amuderson\"),\n",
    "         (\"Lisa\",\"Mocca\"),\n",
    "         (\"Peppermint\",\"Party\"),\n",
    "         (\"Marcy\",\"Green\")]\n",
    "first_name, last_name = zip(*coach)\n",
    "first_name"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cee2104d",
   "metadata": {},
   "source": [
    "#### reversed - reverse structure"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02461f8e",
   "metadata": {},
   "source": [
    "### mutable data structure - dict\n",
    "1. update()直接把新的dict插入到原本的dict中；\n",
    "2. del key关键字或者是pop(key)能够删除相关的记录\n",
    "3. get(key,default_value能够按条件返回一个相关记录\n",
    "4. keys()以及values()能够返回dict所有的key或者是value值；但是需要通过list()实例化\n",
    "5. 创建一个dict：实际上就是两个tuple进行zip操作\n",
    "6. 向dict中插入对象：如果是分拣的方式，那么可以使用setdefault(key,default_key).append(value)\n",
    "7. 或者使用collections中的defaultdict数据结构，直接定义如果不存在改key那么创建什么数据类型来承载这个key中的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "13e7fc7d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0: 9, 1: 8, 2: 7, 3: 6, 4: 5}"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# create a dict from tuple\n",
    "exp_dict3 = dict(zip(range(5),reversed(range(5,10))))\n",
    "exp_dict3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "30d1979b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defaultdict(list,\n",
       "            {'c': ['camellia', 'citrus'],\n",
       "             'a': ['apple', 'apricot'],\n",
       "             'b': ['banana', 'banjiu', 'brown tea']})"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# insert new value into dict\n",
    "from collections import defaultdict\n",
    "\n",
    "words = {\"apple\",\"apricot\",\"banana\",\"banjiu\",\"citrus\",\"camellia\",\"brown tea\"}\n",
    "by_letter = defaultdict(list)\n",
    "for word in words:\n",
    "    by_letter[word[0]].append(word)\n",
    "by_letter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9575f11",
   "metadata": {},
   "source": [
    "#### key selection\n",
    "只有能够哈希化的数据结构能够作为key值，那么mutable的数据结构是不能够作为key值进行存储的，比如list,set,dict"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48884acb",
   "metadata": {},
   "source": [
    "### mutable data structure - set\n",
    "1. {} \n",
    "2. 类似于list，但是其中的数据是不可以重复的，可以用来去重"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5df241d9",
   "metadata": {},
   "source": [
    "### list | set | dict comprehension\n",
    "1. 推断中可以使用map()函数\n",
    "2. list的推断是能够使用到lambda函数的\n",
    "3. 三种数据结构的推断是差不多的，但是list用来筛选或者是map，set用来分拣或者去重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "78c47091",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['APPLY', 'IMPLY', 'PLY', 'FLY', 'LY']"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# list comprehension\n",
    "strings = [\"apply\", 'comply', 'imply', 'dupply', 'ply', 'fly', 'ly']\n",
    "res_strings = [x.upper() for x in strings if len(x) >=2 and len(x) < 6]\n",
    "res_strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "9f4a9fb5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{2, 3, 5, 6}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{2, 3, 5, 6}"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# set comprehension\n",
    "res_length = {len(x) for x in strings} #本质上就是一个map函数\n",
    "res_length2 = set(map(len, strings))\n",
    "\n",
    "print(res_length)\n",
    "res_length2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "827d15e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0: 'apply', 1: 'comply', 2: 'imply', 3: 'dupply', 4: 'ply', 5: 'fly', 6: 'ly'}"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# dict comprehension\n",
    "res_dict = {idx: val for idx,val in enumerate(strings)}\n",
    "res_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "f139d478",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mary', 'David', 'Jake', 'Lucy']"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# nested list|tup comprehension\n",
    "nested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\n",
    "               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\n",
    "# flatten the list\n",
    "flatten_list = [name for names in nested_list for name in names if \"T\" not in name]\n",
    "flatten_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "dcdef2ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('Mary', 'Tom', 'David', 'Thomas'), ('Tim', 'Tong', 'Jake', 'Lucy')]"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# transfer list to tuple\n",
    "res_tuple = [(tuple(name for name in names)) for names in nested_list]\n",
    "res_tuple"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b17c38cc",
   "metadata": {},
   "source": [
    "## namspaces scope and local functions\n",
    "1. 作用域的四个范围\n",
    "    1. local\n",
    "    2. enclosing 嵌套函数中外部函数的变量，可以使用 nonlocal在内部函数中进行调用\n",
    "    3. global 全局变量\n",
    "    4. built-in python解释器使用的变量\n",
    "   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "e3284529",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'__name__': '__main__',\n",
       " '__doc__': 'Automatically created module for IPython interactive environment',\n",
       " '__package__': None,\n",
       " '__loader__': None,\n",
       " '__spec__': None,\n",
       " '__builtin__': <module 'builtins' (built-in)>,\n",
       " '__builtins__': <module 'builtins' (built-in)>,\n",
       " '_ih': ['',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,instance_c,(str,float))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,instance_c,(str,float)))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance((instance_b,instance_c),(str,float)))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = 4\\ninstance_c = 5.5\\nprint(isinstance((instance_b,instance_c),(int,float)))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = 4\\ninstance_c = 5.5\\nprint(isinstance((instance_b,instance_c),(float,float)))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = 4\\ninstance_c = 5.5\\nprint(isinstance((instance_b,int)))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = 4\\ninstance_c = 5.5\\nprint(isinstance(instance_b,int))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,str))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,str))\\nprint(isinstance(instance_c,(int, long ,float)))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,str))\\nprint(isinstance(instance_c,(int, float)))',\n",
       "  '# decide whether the object has __iter__ function - which means it\\'s iterrable\\ninstance_d = \"a string\"\\nprint(isiterable(instance_d))',\n",
       "  'print(\"{:.2f} is float\".format(instance_c))',\n",
       "  '# print formatted resulat\\nprint(\"{:.2f} is float\".format(instance_c))',\n",
       "  '# print formatted results\\nprint(\"{:.2f} is float\".format(instance_c))',\n",
       "  '# print formatted results\\nprint(\"{:.2f} is float\".format(instance_c))',\n",
       "  'from datetime import date, time, datetime',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.month)',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.time)',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)\\nprint(dt.strftime(\"%Y%m%d %h:%m:%s\"))',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)\\nprint(dt.strftime(\"%Y-%m-%d %h:%m:%s\"))',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)\\nprint(dt.strftime(\"%Y-%m-%d %h:%m:%s\"))\\nprint(datetime.strptime(\"20221109194838\",\"%Y-%m-%d %h:%m:%s\"))',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)\\nprint(dt.strftime(\"%Y-%m-%d %h:%m:%s\"))\\nprint(datetime.strptime(\"20221109\",\"%Y-%m-%d\"))',\n",
       "  '# tuple unpacking\\nexp_list = [(1,2,3), (4,5,6), (7,8,9)]\\nfor a,b,c in exp_list:\\n    print(\"a={0}, b={1}, c={2}\".format(a,b,c))',\n",
       "  '# tuple discarding\\nexp_tup = 1,2,3,4,5,6,7\\nprint(exp_tup)\\na,b,*rest = exp_tup\\nrest',\n",
       "  '# tuple common method\\nexp_tup2 = 1,2,3,4,54,3,3,3,3,3,3\\nexp_tup2.count(3)',\n",
       "  '# tuple common method - built-in methods for tuple\\nexp_tup2 = 1,2,3,4,54,3,3,3,3,3,3\\nexp_tup2.count(3)',\n",
       "  '# list could be modified in-place\\nexp_list = [1, 3, 5, 3.6, \"foo\", [3,5]]\\nexp_list',\n",
       "  'exp_list[1] = \"this is changed\"\\nexp_list',\n",
       "  'exp_list1 = [3,6,2,7,12,42,45,87]\\nprint(exp_list1, type(exp_list1))\\nexp_list1.sort()\\nprint(exp_list1, type(exp_list1))',\n",
       "  'exp_list1 = [3,6,2,7,12,42,45,87]\\nprint(exp_list1, type(exp_list1))\\nexp_list1.sort()\\nprint(exp_list1, type(exp_list1))',\n",
       "  'exp_list1 = [3,6,2,7,12,42,45,87]\\nprint(exp_list1, type(exp_list1))\\nexp_list1.sort()\\nprint(exp_list1, type(exp_list1))',\n",
       "  \"def sort_fun(lst):\\n    '''\\n    sort by second element in one input list\\n    '''\\n    return lst[1]\\n\\nexp_list1 = [[3,5],[6,17],[2,23],[7,0],[12,3],[42,41],[45,33],[87,100]]\\nprint(exp_list1, type(exp_list1))\\nexp_list1.sort(key=sort_fun)\\nprint(exp_list1, type(exp_list1))\",\n",
       "  'import bisect\\n\\nbisect_list = [3,23,6,4,3,6,7,12,34,52,5,0,12]\\nbisect_list.sort()\\nres = bisect.bisect(bisect_list,8)\\nprint(res)\\nbisect.insort(bisect_list,8)\\nbisect_list',\n",
       "  'import bisect\\n\\nbisect_list = [3,23,6,4,3,60,7,15,34,52,5,0,12]\\nbisect_list.sort()\\nres = bisect.bisect(bisect_list,8)\\nprint(res)\\nbisect.insort(bisect_list,8)\\nbisect_list',\n",
       "  'import bisect\\n\\nbisect_list = [13,23,6,4,3,60,7,15,34,52,5,0,12]\\nbisect_list.sort()\\nres = bisect.bisect(bisect_list,8)\\nprint(res)\\nbisect.insort(bisect_list,8)\\nbisect_list',\n",
       "  'import bisect\\n\\nbisect_list = [13,23,6,4,3,60,7,15,34,52,5,0,12]\\nbisect_list.sort()\\nres = bisect.bisect(bisect_list,8)\\nprint(res)\\nbisect.insort(bisect_list,8)\\nbisect_list',\n",
       "  '# transfer list into dict\\nexp_dict = {}\\nexp_list = [\"foo\",\\'bar\\',\\'tar\\',\\'war\\',\\'mar\\']\\nfor i,v in enumerate(exp_list):\\n    exp_dict[v] = i\\nexp_dict',\n",
       "  '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nexp_deict2',\n",
       "  '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nexp_dict2',\n",
       "  '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nexp_dict2\\nprint(type(zip(exp_seq1,exp_seq2)))',\n",
       "  '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nprint(exp_dict2)\\nprint(type(zip(exp_seq1,exp_seq2)))\\nprint(zip(exp_seq1,exp_seq2))',\n",
       "  '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nprint(exp_dict2)\\nprint(type(zip(exp_seq1,exp_seq2)))\\nprint(zip(exp_seq1,exp_seq2))\\nprint(list(zip(exp_seq1,exp_seq2)))',\n",
       "  '# zip used for unzip\\ncoach = [(\"Charlie\",\"Brown\"),\\n         (\"Lucy\",\"Amuderson\"),\\n         (\"Lisa\",\"Mocca\"),\\n         (\"Peppermint\",\"Party\"),\\n         (\"Marcy\",\"Green\")]\\nfirst_name, last_name = zip(*coach)\\nfirst_name',\n",
       "  '# zip used for unzip\\n# zip here just combine together all first names and last names\\ncoach = [(\"Charlie\",\"Brown\"),\\n         (\"Lucy\",\"Amuderson\"),\\n         (\"Lisa\",\"Mocca\"),\\n         (\"Peppermint\",\"Party\"),\\n         (\"Marcy\",\"Green\")]\\nfirst_name, last_name = zip(*coach)\\nfirst_name',\n",
       "  '# create a dict from tuple\\nexp_dict3 = dict(zip(range(5),reversed(range(5,10))))',\n",
       "  '# create a dict from tuple\\nexp_dict3 = dict(zip(range(5),reversed(range(5,10))))\\nexp_dict3',\n",
       "  '# insert new value into dict\\nfrom collections import defaultdict\\n\\nwords = {\"apple\",\"apricot\",\"banana\",\"banjiu\",\"citrus\",\"camellia\",\"brown tea\"}\\nby_letter = defaultd0ct(list)\\nfor word in words:\\n    by_letter[word[0]].append(word)\\nby_letter',\n",
       "  '# insert new value into dict\\nfrom collections import defaultdict\\n\\nwords = {\"apple\",\"apricot\",\"banana\",\"banjiu\",\"citrus\",\"camellia\",\"brown tea\"}\\nby_letter = defaultdict(list)\\nfor word in words:\\n    by_letter[word[0]].append(word)\\nby_letter',\n",
       "  '# list comprehension\\nstrings = [\"apply\", \\'comply\\', \\'imply\\', \\'dupply\\', \\'ply\\', \\'fly\\', \\'ly\\']\\nres_strings = [x.upper() for x in strings if len(x) >=2 and len(x) < 5]\\nres_strings',\n",
       "  '# list comprehension\\nstrings = [\"apply\", \\'comply\\', \\'imply\\', \\'dupply\\', \\'ply\\', \\'fly\\', \\'ly\\']\\nres_strings = [x.upper() for x in strings if len(x) >=2 and len(x) < 6]\\nres_strings',\n",
       "  '# set comprehension\\nres_length = {len(x) for x in strings}\\nres_length',\n",
       "  '# dict comprehesion',\n",
       "  '# dict comprehension',\n",
       "  '# set comprehension\\nres_length = {len(x) for x in strings} #本质上就是一个map函数\\nres_length2 = set(map(len, strings))\\nprint(res_length)\\nres_length2',\n",
       "  '# dict comprehension\\nres_dict = {idx: val for ids,val in enumerate(strings)}\\nres_dict',\n",
       "  '# dict comprehension\\nres_dict = {idx: val for idx,val in enumerate(strings)}\\nres_dict',\n",
       "  '# nested list|tup comprehension',\n",
       "  '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if name.contains(T)]',\n",
       "  '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if name.contain(T,t)]',\n",
       "  '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if name.str.contains(T,t)]',\n",
       "  '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if name.contains(T,t)]',\n",
       "  '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if \"T\" not in name]',\n",
       "  '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if \"T\" not in name]\\nflatten_list',\n",
       "  '# transfer list to tuple\\nres_tuple = [(name for name in names) for names in nested_list]\\nres_tuple',\n",
       "  '# transfer list to tuple\\nres_tuple = [(list(name for name in names)) for names in nested_list]\\nres_tuple',\n",
       "  '# transfer list to tuple\\nres_tuple = [(tup(name for name in names)) for names in nested_list]\\nres_tuple',\n",
       "  '# transfer list to tuple\\nres_tuple = [(tuple(name for name in names)) for names in nested_list]\\nres_tuple',\n",
       "  'local()',\n",
       "  'global()',\n",
       "  'global',\n",
       "  'globals()'],\n",
       " '_oh': {25: [3, 4, 5, 6, 7],\n",
       "  26: 7,\n",
       "  27: 7,\n",
       "  28: [1, 'this is changed', 5, 3.6, 'foo', [3, 5]],\n",
       "  29: [1, 'this is changed', 5, 3.6, 'foo', [3, 5]],\n",
       "  34: [0, 3, 3, 4, 5, 6, 6, 7, 8, 12, 12, 23, 34, 52],\n",
       "  35: [0, 3, 3, 4, 5, 6, 7, 8, 12, 15, 23, 34, 52, 60],\n",
       "  36: [0, 3, 4, 5, 6, 7, 8, 12, 13, 15, 23, 34, 52, 60],\n",
       "  37: [0, 3, 4, 5, 6, 7, 8, 12, 13, 15, 23, 34, 52, 60],\n",
       "  38: {'foo': 0, 'bar': 1, 'tar': 2, 'war': 3, 'mar': 4},\n",
       "  40: {0: ('foo', 'A'), 1: ('bar', 'B'), 2: ('tar', 'C'), 3: ('war', 'D')},\n",
       "  44: ('Charlie', 'Lucy', 'Lisa', 'Peppermint', 'Marcy'),\n",
       "  45: ('Charlie', 'Lucy', 'Lisa', 'Peppermint', 'Marcy'),\n",
       "  47: {0: 9, 1: 8, 2: 7, 3: 6, 4: 5},\n",
       "  49: defaultdict(list,\n",
       "              {'c': ['camellia', 'citrus'],\n",
       "               'a': ['apple', 'apricot'],\n",
       "               'b': ['banana', 'banjiu', 'brown tea']}),\n",
       "  50: ['PLY', 'FLY', 'LY'],\n",
       "  51: ['APPLY', 'IMPLY', 'PLY', 'FLY', 'LY'],\n",
       "  52: {2, 3, 5, 6},\n",
       "  55: {2, 3, 5, 6},\n",
       "  57: {0: 'apply',\n",
       "   1: 'comply',\n",
       "   2: 'imply',\n",
       "   3: 'dupply',\n",
       "   4: 'ply',\n",
       "   5: 'fly',\n",
       "   6: 'ly'},\n",
       "  64: ['Mary', 'David', 'Jake', 'Lucy'],\n",
       "  65: [<generator object <listcomp>.<genexpr> at 0x7fb1a3b89d60>,\n",
       "   <generator object <listcomp>.<genexpr> at 0x7fb1a3b89eb0>],\n",
       "  66: [['Mary', 'Tom', 'David', 'Thomas'], ['Tim', 'Tong', 'Jake', 'Lucy']],\n",
       "  68: [('Mary', 'Tom', 'David', 'Thomas'), ('Tim', 'Tong', 'Jake', 'Lucy')]},\n",
       " '_dh': ['/Users/collinsliu/jupyter/python_learning_basics/python for data analysis'],\n",
       " 'In': ['',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,instance_c,(str,float))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,instance_c,(str,float)))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance((instance_b,instance_c),(str,float)))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = 4\\ninstance_c = 5.5\\nprint(isinstance((instance_b,instance_c),(int,float)))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = 4\\ninstance_c = 5.5\\nprint(isinstance((instance_b,instance_c),(float,float)))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = 4\\ninstance_c = 5.5\\nprint(isinstance((instance_b,int)))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = 4\\ninstance_c = 5.5\\nprint(isinstance(instance_b,int))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,str))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,str))\\nprint(isinstance(instance_c,(int, long ,float)))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,str))\\nprint(isinstance(instance_c,(int, float)))',\n",
       "  '# decide whether the object has __iter__ function - which means it\\'s iterrable\\ninstance_d = \"a string\"\\nprint(isiterable(instance_d))',\n",
       "  'print(\"{:.2f} is float\".format(instance_c))',\n",
       "  '# print formatted resulat\\nprint(\"{:.2f} is float\".format(instance_c))',\n",
       "  '# print formatted results\\nprint(\"{:.2f} is float\".format(instance_c))',\n",
       "  '# print formatted results\\nprint(\"{:.2f} is float\".format(instance_c))',\n",
       "  'from datetime import date, time, datetime',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.month)',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.time)',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)\\nprint(dt.strftime(\"%Y%m%d %h:%m:%s\"))',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)\\nprint(dt.strftime(\"%Y-%m-%d %h:%m:%s\"))',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)\\nprint(dt.strftime(\"%Y-%m-%d %h:%m:%s\"))\\nprint(datetime.strptime(\"20221109194838\",\"%Y-%m-%d %h:%m:%s\"))',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)\\nprint(dt.strftime(\"%Y-%m-%d %h:%m:%s\"))\\nprint(datetime.strptime(\"20221109\",\"%Y-%m-%d\"))',\n",
       "  '# tuple unpacking\\nexp_list = [(1,2,3), (4,5,6), (7,8,9)]\\nfor a,b,c in exp_list:\\n    print(\"a={0}, b={1}, c={2}\".format(a,b,c))',\n",
       "  '# tuple discarding\\nexp_tup = 1,2,3,4,5,6,7\\nprint(exp_tup)\\na,b,*rest = exp_tup\\nrest',\n",
       "  '# tuple common method\\nexp_tup2 = 1,2,3,4,54,3,3,3,3,3,3\\nexp_tup2.count(3)',\n",
       "  '# tuple common method - built-in methods for tuple\\nexp_tup2 = 1,2,3,4,54,3,3,3,3,3,3\\nexp_tup2.count(3)',\n",
       "  '# list could be modified in-place\\nexp_list = [1, 3, 5, 3.6, \"foo\", [3,5]]\\nexp_list',\n",
       "  'exp_list[1] = \"this is changed\"\\nexp_list',\n",
       "  'exp_list1 = [3,6,2,7,12,42,45,87]\\nprint(exp_list1, type(exp_list1))\\nexp_list1.sort()\\nprint(exp_list1, type(exp_list1))',\n",
       "  'exp_list1 = [3,6,2,7,12,42,45,87]\\nprint(exp_list1, type(exp_list1))\\nexp_list1.sort()\\nprint(exp_list1, type(exp_list1))',\n",
       "  'exp_list1 = [3,6,2,7,12,42,45,87]\\nprint(exp_list1, type(exp_list1))\\nexp_list1.sort()\\nprint(exp_list1, type(exp_list1))',\n",
       "  \"def sort_fun(lst):\\n    '''\\n    sort by second element in one input list\\n    '''\\n    return lst[1]\\n\\nexp_list1 = [[3,5],[6,17],[2,23],[7,0],[12,3],[42,41],[45,33],[87,100]]\\nprint(exp_list1, type(exp_list1))\\nexp_list1.sort(key=sort_fun)\\nprint(exp_list1, type(exp_list1))\",\n",
       "  'import bisect\\n\\nbisect_list = [3,23,6,4,3,6,7,12,34,52,5,0,12]\\nbisect_list.sort()\\nres = bisect.bisect(bisect_list,8)\\nprint(res)\\nbisect.insort(bisect_list,8)\\nbisect_list',\n",
       "  'import bisect\\n\\nbisect_list = [3,23,6,4,3,60,7,15,34,52,5,0,12]\\nbisect_list.sort()\\nres = bisect.bisect(bisect_list,8)\\nprint(res)\\nbisect.insort(bisect_list,8)\\nbisect_list',\n",
       "  'import bisect\\n\\nbisect_list = [13,23,6,4,3,60,7,15,34,52,5,0,12]\\nbisect_list.sort()\\nres = bisect.bisect(bisect_list,8)\\nprint(res)\\nbisect.insort(bisect_list,8)\\nbisect_list',\n",
       "  'import bisect\\n\\nbisect_list = [13,23,6,4,3,60,7,15,34,52,5,0,12]\\nbisect_list.sort()\\nres = bisect.bisect(bisect_list,8)\\nprint(res)\\nbisect.insort(bisect_list,8)\\nbisect_list',\n",
       "  '# transfer list into dict\\nexp_dict = {}\\nexp_list = [\"foo\",\\'bar\\',\\'tar\\',\\'war\\',\\'mar\\']\\nfor i,v in enumerate(exp_list):\\n    exp_dict[v] = i\\nexp_dict',\n",
       "  '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nexp_deict2',\n",
       "  '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nexp_dict2',\n",
       "  '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nexp_dict2\\nprint(type(zip(exp_seq1,exp_seq2)))',\n",
       "  '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nprint(exp_dict2)\\nprint(type(zip(exp_seq1,exp_seq2)))\\nprint(zip(exp_seq1,exp_seq2))',\n",
       "  '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nprint(exp_dict2)\\nprint(type(zip(exp_seq1,exp_seq2)))\\nprint(zip(exp_seq1,exp_seq2))\\nprint(list(zip(exp_seq1,exp_seq2)))',\n",
       "  '# zip used for unzip\\ncoach = [(\"Charlie\",\"Brown\"),\\n         (\"Lucy\",\"Amuderson\"),\\n         (\"Lisa\",\"Mocca\"),\\n         (\"Peppermint\",\"Party\"),\\n         (\"Marcy\",\"Green\")]\\nfirst_name, last_name = zip(*coach)\\nfirst_name',\n",
       "  '# zip used for unzip\\n# zip here just combine together all first names and last names\\ncoach = [(\"Charlie\",\"Brown\"),\\n         (\"Lucy\",\"Amuderson\"),\\n         (\"Lisa\",\"Mocca\"),\\n         (\"Peppermint\",\"Party\"),\\n         (\"Marcy\",\"Green\")]\\nfirst_name, last_name = zip(*coach)\\nfirst_name',\n",
       "  '# create a dict from tuple\\nexp_dict3 = dict(zip(range(5),reversed(range(5,10))))',\n",
       "  '# create a dict from tuple\\nexp_dict3 = dict(zip(range(5),reversed(range(5,10))))\\nexp_dict3',\n",
       "  '# insert new value into dict\\nfrom collections import defaultdict\\n\\nwords = {\"apple\",\"apricot\",\"banana\",\"banjiu\",\"citrus\",\"camellia\",\"brown tea\"}\\nby_letter = defaultd0ct(list)\\nfor word in words:\\n    by_letter[word[0]].append(word)\\nby_letter',\n",
       "  '# insert new value into dict\\nfrom collections import defaultdict\\n\\nwords = {\"apple\",\"apricot\",\"banana\",\"banjiu\",\"citrus\",\"camellia\",\"brown tea\"}\\nby_letter = defaultdict(list)\\nfor word in words:\\n    by_letter[word[0]].append(word)\\nby_letter',\n",
       "  '# list comprehension\\nstrings = [\"apply\", \\'comply\\', \\'imply\\', \\'dupply\\', \\'ply\\', \\'fly\\', \\'ly\\']\\nres_strings = [x.upper() for x in strings if len(x) >=2 and len(x) < 5]\\nres_strings',\n",
       "  '# list comprehension\\nstrings = [\"apply\", \\'comply\\', \\'imply\\', \\'dupply\\', \\'ply\\', \\'fly\\', \\'ly\\']\\nres_strings = [x.upper() for x in strings if len(x) >=2 and len(x) < 6]\\nres_strings',\n",
       "  '# set comprehension\\nres_length = {len(x) for x in strings}\\nres_length',\n",
       "  '# dict comprehesion',\n",
       "  '# dict comprehension',\n",
       "  '# set comprehension\\nres_length = {len(x) for x in strings} #本质上就是一个map函数\\nres_length2 = set(map(len, strings))\\nprint(res_length)\\nres_length2',\n",
       "  '# dict comprehension\\nres_dict = {idx: val for ids,val in enumerate(strings)}\\nres_dict',\n",
       "  '# dict comprehension\\nres_dict = {idx: val for idx,val in enumerate(strings)}\\nres_dict',\n",
       "  '# nested list|tup comprehension',\n",
       "  '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if name.contains(T)]',\n",
       "  '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if name.contain(T,t)]',\n",
       "  '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if name.str.contains(T,t)]',\n",
       "  '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if name.contains(T,t)]',\n",
       "  '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if \"T\" not in name]',\n",
       "  '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if \"T\" not in name]\\nflatten_list',\n",
       "  '# transfer list to tuple\\nres_tuple = [(name for name in names) for names in nested_list]\\nres_tuple',\n",
       "  '# transfer list to tuple\\nres_tuple = [(list(name for name in names)) for names in nested_list]\\nres_tuple',\n",
       "  '# transfer list to tuple\\nres_tuple = [(tup(name for name in names)) for names in nested_list]\\nres_tuple',\n",
       "  '# transfer list to tuple\\nres_tuple = [(tuple(name for name in names)) for names in nested_list]\\nres_tuple',\n",
       "  'local()',\n",
       "  'global()',\n",
       "  'global',\n",
       "  'globals()'],\n",
       " 'Out': {25: [3, 4, 5, 6, 7],\n",
       "  26: 7,\n",
       "  27: 7,\n",
       "  28: [1, 'this is changed', 5, 3.6, 'foo', [3, 5]],\n",
       "  29: [1, 'this is changed', 5, 3.6, 'foo', [3, 5]],\n",
       "  34: [0, 3, 3, 4, 5, 6, 6, 7, 8, 12, 12, 23, 34, 52],\n",
       "  35: [0, 3, 3, 4, 5, 6, 7, 8, 12, 15, 23, 34, 52, 60],\n",
       "  36: [0, 3, 4, 5, 6, 7, 8, 12, 13, 15, 23, 34, 52, 60],\n",
       "  37: [0, 3, 4, 5, 6, 7, 8, 12, 13, 15, 23, 34, 52, 60],\n",
       "  38: {'foo': 0, 'bar': 1, 'tar': 2, 'war': 3, 'mar': 4},\n",
       "  40: {0: ('foo', 'A'), 1: ('bar', 'B'), 2: ('tar', 'C'), 3: ('war', 'D')},\n",
       "  44: ('Charlie', 'Lucy', 'Lisa', 'Peppermint', 'Marcy'),\n",
       "  45: ('Charlie', 'Lucy', 'Lisa', 'Peppermint', 'Marcy'),\n",
       "  47: {0: 9, 1: 8, 2: 7, 3: 6, 4: 5},\n",
       "  49: defaultdict(list,\n",
       "              {'c': ['camellia', 'citrus'],\n",
       "               'a': ['apple', 'apricot'],\n",
       "               'b': ['banana', 'banjiu', 'brown tea']}),\n",
       "  50: ['PLY', 'FLY', 'LY'],\n",
       "  51: ['APPLY', 'IMPLY', 'PLY', 'FLY', 'LY'],\n",
       "  52: {2, 3, 5, 6},\n",
       "  55: {2, 3, 5, 6},\n",
       "  57: {0: 'apply',\n",
       "   1: 'comply',\n",
       "   2: 'imply',\n",
       "   3: 'dupply',\n",
       "   4: 'ply',\n",
       "   5: 'fly',\n",
       "   6: 'ly'},\n",
       "  64: ['Mary', 'David', 'Jake', 'Lucy'],\n",
       "  65: [<generator object <listcomp>.<genexpr> at 0x7fb1a3b89d60>,\n",
       "   <generator object <listcomp>.<genexpr> at 0x7fb1a3b89eb0>],\n",
       "  66: [['Mary', 'Tom', 'David', 'Thomas'], ['Tim', 'Tong', 'Jake', 'Lucy']],\n",
       "  68: [('Mary', 'Tom', 'David', 'Thomas'), ('Tim', 'Tong', 'Jake', 'Lucy')]},\n",
       " 'get_ipython': <bound method InteractiveShell.get_ipython of <ipykernel.zmqshell.ZMQInteractiveShell object at 0x7fb1a192f040>>,\n",
       " 'exit': <IPython.core.autocall.ZMQExitAutocall at 0x7fb1a19f0250>,\n",
       " 'quit': <IPython.core.autocall.ZMQExitAutocall at 0x7fb1a19f0250>,\n",
       " '_': [('Mary', 'Tom', 'David', 'Thomas'), ('Tim', 'Tong', 'Jake', 'Lucy')],\n",
       " '__': [['Mary', 'Tom', 'David', 'Thomas'], ['Tim', 'Tong', 'Jake', 'Lucy']],\n",
       " '___': [<generator object <listcomp>.<genexpr> at 0x7fb1a3b89d60>,\n",
       "  <generator object <listcomp>.<genexpr> at 0x7fb1a3b89eb0>],\n",
       " '_i': 'global',\n",
       " '_ii': 'global()',\n",
       " '_iii': 'local()',\n",
       " '_i1': '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,instance_c,(str,float))',\n",
       " '_i2': '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,instance_c,(str,float)))',\n",
       " 'instance_a': 5,\n",
       " 'instance_b': 'name',\n",
       " 'instance_c': 5.5,\n",
       " '_i3': '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance((instance_b,instance_c),(str,float)))',\n",
       " '_i4': '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = 4\\ninstance_c = 5.5\\nprint(isinstance((instance_b,instance_c),(int,float)))',\n",
       " '_i5': '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = 4\\ninstance_c = 5.5\\nprint(isinstance((instance_b,instance_c),(float,float)))',\n",
       " '_i6': '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = 4\\ninstance_c = 5.5\\nprint(isinstance((instance_b,int)))',\n",
       " '_i7': '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = 4\\ninstance_c = 5.5\\nprint(isinstance(instance_b,int))',\n",
       " '_i8': '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,str))',\n",
       " '_i9': '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,str))\\nprint(isinstance(instance_c,(int, long ,float)))',\n",
       " '_i10': '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,str))\\nprint(isinstance(instance_c,(int, float)))',\n",
       " '_i11': '# decide whether the object has __iter__ function - which means it\\'s iterrable\\ninstance_d = \"a string\"\\nprint(isiterable(instance_d))',\n",
       " 'instance_d': 'a string',\n",
       " '_i12': 'print(\"{:.2f} is float\".format(instance_c))',\n",
       " '_i13': '# print formatted resulat\\nprint(\"{:.2f} is float\".format(instance_c))',\n",
       " '_i14': '# print formatted results\\nprint(\"{:.2f} is float\".format(instance_c))',\n",
       " '_i15': '# print formatted results\\nprint(\"{:.2f} is float\".format(instance_c))',\n",
       " '_i16': 'from datetime import date, time, datetime',\n",
       " 'date': datetime.date,\n",
       " 'time': datetime.time,\n",
       " 'datetime': datetime.datetime,\n",
       " '_i17': 'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.month)',\n",
       " 'dt': datetime.datetime(2022, 11, 9, 19, 46, 50),\n",
       " '_i18': 'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.time)',\n",
       " '_i19': 'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)',\n",
       " '_i20': 'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)\\nprint(dt.strftime(\"%Y%m%d %h:%m:%s\"))',\n",
       " '_i21': 'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)\\nprint(dt.strftime(\"%Y-%m-%d %h:%m:%s\"))',\n",
       " '_i22': 'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)\\nprint(dt.strftime(\"%Y-%m-%d %h:%m:%s\"))\\nprint(datetime.strptime(\"20221109194838\",\"%Y-%m-%d %h:%m:%s\"))',\n",
       " '_i23': 'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)\\nprint(dt.strftime(\"%Y-%m-%d %h:%m:%s\"))\\nprint(datetime.strptime(\"20221109\",\"%Y-%m-%d\"))',\n",
       " '_i24': '# tuple unpacking\\nexp_list = [(1,2,3), (4,5,6), (7,8,9)]\\nfor a,b,c in exp_list:\\n    print(\"a={0}, b={1}, c={2}\".format(a,b,c))',\n",
       " 'exp_list': ['foo', 'bar', 'tar', 'war', 'mar'],\n",
       " 'a': 1,\n",
       " 'b': 2,\n",
       " 'c': 9,\n",
       " '_i25': '# tuple discarding\\nexp_tup = 1,2,3,4,5,6,7\\nprint(exp_tup)\\na,b,*rest = exp_tup\\nrest',\n",
       " 'exp_tup': (1, 2, 3, 4, 5, 6, 7),\n",
       " 'rest': [3, 4, 5, 6, 7],\n",
       " '_25': [3, 4, 5, 6, 7],\n",
       " '_i26': '# tuple common method\\nexp_tup2 = 1,2,3,4,54,3,3,3,3,3,3\\nexp_tup2.count(3)',\n",
       " 'exp_tup2': (1, 2, 3, 4, 54, 3, 3, 3, 3, 3, 3),\n",
       " '_26': 7,\n",
       " '_i27': '# tuple common method - built-in methods for tuple\\nexp_tup2 = 1,2,3,4,54,3,3,3,3,3,3\\nexp_tup2.count(3)',\n",
       " '_27': 7,\n",
       " '_i28': '# list could be modified in-place\\nexp_list = [1, 3, 5, 3.6, \"foo\", [3,5]]\\nexp_list',\n",
       " '_28': [1, 'this is changed', 5, 3.6, 'foo', [3, 5]],\n",
       " '_i29': 'exp_list[1] = \"this is changed\"\\nexp_list',\n",
       " '_29': [1, 'this is changed', 5, 3.6, 'foo', [3, 5]],\n",
       " '_i30': 'exp_list1 = [3,6,2,7,12,42,45,87]\\nprint(exp_list1, type(exp_list1))\\nexp_list1.sort()\\nprint(exp_list1, type(exp_list1))',\n",
       " 'exp_list1': [[7, 0],\n",
       "  [12, 3],\n",
       "  [3, 5],\n",
       "  [6, 17],\n",
       "  [2, 23],\n",
       "  [45, 33],\n",
       "  [42, 41],\n",
       "  [87, 100]],\n",
       " '_i31': 'exp_list1 = [3,6,2,7,12,42,45,87]\\nprint(exp_list1, type(exp_list1))\\nexp_list1.sort()\\nprint(exp_list1, type(exp_list1))',\n",
       " '_i32': 'exp_list1 = [3,6,2,7,12,42,45,87]\\nprint(exp_list1, type(exp_list1))\\nexp_list1.sort()\\nprint(exp_list1, type(exp_list1))',\n",
       " '_i33': \"def sort_fun(lst):\\n    '''\\n    sort by second element in one input list\\n    '''\\n    return lst[1]\\n\\nexp_list1 = [[3,5],[6,17],[2,23],[7,0],[12,3],[42,41],[45,33],[87,100]]\\nprint(exp_list1, type(exp_list1))\\nexp_list1.sort(key=sort_fun)\\nprint(exp_list1, type(exp_list1))\",\n",
       " 'sort_fun': <function __main__.sort_fun(lst)>,\n",
       " '_i34': 'import bisect\\n\\nbisect_list = [3,23,6,4,3,6,7,12,34,52,5,0,12]\\nbisect_list.sort()\\nres = bisect.bisect(bisect_list,8)\\nprint(res)\\nbisect.insort(bisect_list,8)\\nbisect_list',\n",
       " 'bisect': <module 'bisect' from '/Users/collinsliu/opt/anaconda3/lib/python3.9/bisect.py'>,\n",
       " 'bisect_list': [0, 3, 4, 5, 6, 7, 8, 12, 13, 15, 23, 34, 52, 60],\n",
       " 'res': 6,\n",
       " '_34': [0, 3, 3, 4, 5, 6, 6, 7, 8, 12, 12, 23, 34, 52],\n",
       " '_i35': 'import bisect\\n\\nbisect_list = [3,23,6,4,3,60,7,15,34,52,5,0,12]\\nbisect_list.sort()\\nres = bisect.bisect(bisect_list,8)\\nprint(res)\\nbisect.insort(bisect_list,8)\\nbisect_list',\n",
       " '_35': [0, 3, 3, 4, 5, 6, 7, 8, 12, 15, 23, 34, 52, 60],\n",
       " '_i36': 'import bisect\\n\\nbisect_list = [13,23,6,4,3,60,7,15,34,52,5,0,12]\\nbisect_list.sort()\\nres = bisect.bisect(bisect_list,8)\\nprint(res)\\nbisect.insort(bisect_list,8)\\nbisect_list',\n",
       " '_36': [0, 3, 4, 5, 6, 7, 8, 12, 13, 15, 23, 34, 52, 60],\n",
       " '_i37': 'import bisect\\n\\nbisect_list = [13,23,6,4,3,60,7,15,34,52,5,0,12]\\nbisect_list.sort()\\nres = bisect.bisect(bisect_list,8)\\nprint(res)\\nbisect.insort(bisect_list,8)\\nbisect_list',\n",
       " '_37': [0, 3, 4, 5, 6, 7, 8, 12, 13, 15, 23, 34, 52, 60],\n",
       " '_i38': '# transfer list into dict\\nexp_dict = {}\\nexp_list = [\"foo\",\\'bar\\',\\'tar\\',\\'war\\',\\'mar\\']\\nfor i,v in enumerate(exp_list):\\n    exp_dict[v] = i\\nexp_dict',\n",
       " 'exp_dict': {'foo': 0, 'bar': 1, 'tar': 2, 'war': 3, 'mar': 4},\n",
       " 'i': 3,\n",
       " 'v': ('war', 'D'),\n",
       " '_38': {'foo': 0, 'bar': 1, 'tar': 2, 'war': 3, 'mar': 4},\n",
       " '_i39': '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nexp_deict2',\n",
       " 'exp_seq1': ('foo', 'bar', 'tar', 'war'),\n",
       " 'exp_seq2': ('A', 'B', 'C', 'D'),\n",
       " 'exp_dict2': {0: ('foo', 'A'),\n",
       "  1: ('bar', 'B'),\n",
       "  2: ('tar', 'C'),\n",
       "  3: ('war', 'D')},\n",
       " '_i40': '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nexp_dict2',\n",
       " '_40': {0: ('foo', 'A'), 1: ('bar', 'B'), 2: ('tar', 'C'), 3: ('war', 'D')},\n",
       " '_i41': '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nexp_dict2\\nprint(type(zip(exp_seq1,exp_seq2)))',\n",
       " '_i42': '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nprint(exp_dict2)\\nprint(type(zip(exp_seq1,exp_seq2)))\\nprint(zip(exp_seq1,exp_seq2))',\n",
       " '_i43': '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nprint(exp_dict2)\\nprint(type(zip(exp_seq1,exp_seq2)))\\nprint(zip(exp_seq1,exp_seq2))\\nprint(list(zip(exp_seq1,exp_seq2)))',\n",
       " '_i44': '# zip used for unzip\\ncoach = [(\"Charlie\",\"Brown\"),\\n         (\"Lucy\",\"Amuderson\"),\\n         (\"Lisa\",\"Mocca\"),\\n         (\"Peppermint\",\"Party\"),\\n         (\"Marcy\",\"Green\")]\\nfirst_name, last_name = zip(*coach)\\nfirst_name',\n",
       " 'coach': [('Charlie', 'Brown'),\n",
       "  ('Lucy', 'Amuderson'),\n",
       "  ('Lisa', 'Mocca'),\n",
       "  ('Peppermint', 'Party'),\n",
       "  ('Marcy', 'Green')],\n",
       " 'first_name': ('Charlie', 'Lucy', 'Lisa', 'Peppermint', 'Marcy'),\n",
       " 'last_name': ('Brown', 'Amuderson', 'Mocca', 'Party', 'Green'),\n",
       " '_44': ('Charlie', 'Lucy', 'Lisa', 'Peppermint', 'Marcy'),\n",
       " '_i45': '# zip used for unzip\\n# zip here just combine together all first names and last names\\ncoach = [(\"Charlie\",\"Brown\"),\\n         (\"Lucy\",\"Amuderson\"),\\n         (\"Lisa\",\"Mocca\"),\\n         (\"Peppermint\",\"Party\"),\\n         (\"Marcy\",\"Green\")]\\nfirst_name, last_name = zip(*coach)\\nfirst_name',\n",
       " '_45': ('Charlie', 'Lucy', 'Lisa', 'Peppermint', 'Marcy'),\n",
       " '_i46': '# create a dict from tuple\\nexp_dict3 = dict(zip(range(5),reversed(range(5,10))))',\n",
       " 'exp_dict3': {0: 9, 1: 8, 2: 7, 3: 6, 4: 5},\n",
       " '_i47': '# create a dict from tuple\\nexp_dict3 = dict(zip(range(5),reversed(range(5,10))))\\nexp_dict3',\n",
       " '_47': {0: 9, 1: 8, 2: 7, 3: 6, 4: 5},\n",
       " '_i48': '# insert new value into dict\\nfrom collections import defaultdict\\n\\nwords = {\"apple\",\"apricot\",\"banana\",\"banjiu\",\"citrus\",\"camellia\",\"brown tea\"}\\nby_letter = defaultd0ct(list)\\nfor word in words:\\n    by_letter[word[0]].append(word)\\nby_letter',\n",
       " 'defaultdict': collections.defaultdict,\n",
       " 'words': {'apple',\n",
       "  'apricot',\n",
       "  'banana',\n",
       "  'banjiu',\n",
       "  'brown tea',\n",
       "  'camellia',\n",
       "  'citrus'},\n",
       " '_i49': '# insert new value into dict\\nfrom collections import defaultdict\\n\\nwords = {\"apple\",\"apricot\",\"banana\",\"banjiu\",\"citrus\",\"camellia\",\"brown tea\"}\\nby_letter = defaultdict(list)\\nfor word in words:\\n    by_letter[word[0]].append(word)\\nby_letter',\n",
       " 'by_letter': defaultdict(list,\n",
       "             {'c': ['camellia', 'citrus'],\n",
       "              'a': ['apple', 'apricot'],\n",
       "              'b': ['banana', 'banjiu', 'brown tea']}),\n",
       " 'word': 'apricot',\n",
       " '_49': defaultdict(list,\n",
       "             {'c': ['camellia', 'citrus'],\n",
       "              'a': ['apple', 'apricot'],\n",
       "              'b': ['banana', 'banjiu', 'brown tea']}),\n",
       " '_i50': '# list comprehension\\nstrings = [\"apply\", \\'comply\\', \\'imply\\', \\'dupply\\', \\'ply\\', \\'fly\\', \\'ly\\']\\nres_strings = [x.upper() for x in strings if len(x) >=2 and len(x) < 5]\\nres_strings',\n",
       " 'strings': ['apply', 'comply', 'imply', 'dupply', 'ply', 'fly', 'ly'],\n",
       " 'res_strings': ['APPLY', 'IMPLY', 'PLY', 'FLY', 'LY'],\n",
       " '_50': ['PLY', 'FLY', 'LY'],\n",
       " '_i51': '# list comprehension\\nstrings = [\"apply\", \\'comply\\', \\'imply\\', \\'dupply\\', \\'ply\\', \\'fly\\', \\'ly\\']\\nres_strings = [x.upper() for x in strings if len(x) >=2 and len(x) < 6]\\nres_strings',\n",
       " '_51': ['APPLY', 'IMPLY', 'PLY', 'FLY', 'LY'],\n",
       " '_i52': '# set comprehension\\nres_length = {len(x) for x in strings}\\nres_length',\n",
       " 'res_length': {2, 3, 5, 6},\n",
       " '_52': {2, 3, 5, 6},\n",
       " '_i53': '# dict comprehesion',\n",
       " '_i54': '# dict comprehension',\n",
       " '_i55': '# set comprehension\\nres_length = {len(x) for x in strings} #本质上就是一个map函数\\nres_length2 = set(map(len, strings))\\nprint(res_length)\\nres_length2',\n",
       " 'res_length2': {2, 3, 5, 6},\n",
       " '_55': {2, 3, 5, 6},\n",
       " '_i56': '# dict comprehension\\nres_dict = {idx: val for ids,val in enumerate(strings)}\\nres_dict',\n",
       " '_i57': '# dict comprehension\\nres_dict = {idx: val for idx,val in enumerate(strings)}\\nres_dict',\n",
       " 'res_dict': {0: 'apply',\n",
       "  1: 'comply',\n",
       "  2: 'imply',\n",
       "  3: 'dupply',\n",
       "  4: 'ply',\n",
       "  5: 'fly',\n",
       "  6: 'ly'},\n",
       " '_57': {0: 'apply',\n",
       "  1: 'comply',\n",
       "  2: 'imply',\n",
       "  3: 'dupply',\n",
       "  4: 'ply',\n",
       "  5: 'fly',\n",
       "  6: 'ly'},\n",
       " '_i58': '# nested list|tup comprehension',\n",
       " '_i59': '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if name.contains(T)]',\n",
       " 'nested_list': [['Mary', 'Tom', 'David', 'Thomas'],\n",
       "  ['Tim', 'Tong', 'Jake', 'Lucy']],\n",
       " '_i60': '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if name.contain(T,t)]',\n",
       " '_i61': '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if name.str.contains(T,t)]',\n",
       " '_i62': '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if name.contains(T,t)]',\n",
       " '_i63': '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if \"T\" not in name]',\n",
       " 'flatten_list': ['Mary', 'David', 'Jake', 'Lucy'],\n",
       " '_i64': '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if \"T\" not in name]\\nflatten_list',\n",
       " '_64': ['Mary', 'David', 'Jake', 'Lucy'],\n",
       " '_i65': '# transfer list to tuple\\nres_tuple = [(name for name in names) for names in nested_list]\\nres_tuple',\n",
       " 'res_tuple': [('Mary', 'Tom', 'David', 'Thomas'),\n",
       "  ('Tim', 'Tong', 'Jake', 'Lucy')],\n",
       " '_65': [<generator object <listcomp>.<genexpr> at 0x7fb1a3b89d60>,\n",
       "  <generator object <listcomp>.<genexpr> at 0x7fb1a3b89eb0>],\n",
       " '_i66': '# transfer list to tuple\\nres_tuple = [(list(name for name in names)) for names in nested_list]\\nres_tuple',\n",
       " '_66': [['Mary', 'Tom', 'David', 'Thomas'], ['Tim', 'Tong', 'Jake', 'Lucy']],\n",
       " '_i67': '# transfer list to tuple\\nres_tuple = [(tup(name for name in names)) for names in nested_list]\\nres_tuple',\n",
       " '_i68': '# transfer list to tuple\\nres_tuple = [(tuple(name for name in names)) for names in nested_list]\\nres_tuple',\n",
       " '_68': [('Mary', 'Tom', 'David', 'Thomas'), ('Tim', 'Tong', 'Jake', 'Lucy')],\n",
       " '_i69': 'local()',\n",
       " '_i70': 'global()',\n",
       " '_i71': 'global',\n",
       " '_i72': 'globals()'}"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "globals()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "88a3d99a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'__name__': '__main__',\n",
       " '__doc__': 'Automatically created module for IPython interactive environment',\n",
       " '__package__': None,\n",
       " '__loader__': None,\n",
       " '__spec__': None,\n",
       " '__builtin__': <module 'builtins' (built-in)>,\n",
       " '__builtins__': <module 'builtins' (built-in)>,\n",
       " '_ih': ['',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,instance_c,(str,float))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,instance_c,(str,float)))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance((instance_b,instance_c),(str,float)))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = 4\\ninstance_c = 5.5\\nprint(isinstance((instance_b,instance_c),(int,float)))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = 4\\ninstance_c = 5.5\\nprint(isinstance((instance_b,instance_c),(float,float)))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = 4\\ninstance_c = 5.5\\nprint(isinstance((instance_b,int)))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = 4\\ninstance_c = 5.5\\nprint(isinstance(instance_b,int))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,str))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,str))\\nprint(isinstance(instance_c,(int, long ,float)))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,str))\\nprint(isinstance(instance_c,(int, float)))',\n",
       "  '# decide whether the object has __iter__ function - which means it\\'s iterrable\\ninstance_d = \"a string\"\\nprint(isiterable(instance_d))',\n",
       "  'print(\"{:.2f} is float\".format(instance_c))',\n",
       "  '# print formatted resulat\\nprint(\"{:.2f} is float\".format(instance_c))',\n",
       "  '# print formatted results\\nprint(\"{:.2f} is float\".format(instance_c))',\n",
       "  '# print formatted results\\nprint(\"{:.2f} is float\".format(instance_c))',\n",
       "  'from datetime import date, time, datetime',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.month)',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.time)',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)\\nprint(dt.strftime(\"%Y%m%d %h:%m:%s\"))',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)\\nprint(dt.strftime(\"%Y-%m-%d %h:%m:%s\"))',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)\\nprint(dt.strftime(\"%Y-%m-%d %h:%m:%s\"))\\nprint(datetime.strptime(\"20221109194838\",\"%Y-%m-%d %h:%m:%s\"))',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)\\nprint(dt.strftime(\"%Y-%m-%d %h:%m:%s\"))\\nprint(datetime.strptime(\"20221109\",\"%Y-%m-%d\"))',\n",
       "  '# tuple unpacking\\nexp_list = [(1,2,3), (4,5,6), (7,8,9)]\\nfor a,b,c in exp_list:\\n    print(\"a={0}, b={1}, c={2}\".format(a,b,c))',\n",
       "  '# tuple discarding\\nexp_tup = 1,2,3,4,5,6,7\\nprint(exp_tup)\\na,b,*rest = exp_tup\\nrest',\n",
       "  '# tuple common method\\nexp_tup2 = 1,2,3,4,54,3,3,3,3,3,3\\nexp_tup2.count(3)',\n",
       "  '# tuple common method - built-in methods for tuple\\nexp_tup2 = 1,2,3,4,54,3,3,3,3,3,3\\nexp_tup2.count(3)',\n",
       "  '# list could be modified in-place\\nexp_list = [1, 3, 5, 3.6, \"foo\", [3,5]]\\nexp_list',\n",
       "  'exp_list[1] = \"this is changed\"\\nexp_list',\n",
       "  'exp_list1 = [3,6,2,7,12,42,45,87]\\nprint(exp_list1, type(exp_list1))\\nexp_list1.sort()\\nprint(exp_list1, type(exp_list1))',\n",
       "  'exp_list1 = [3,6,2,7,12,42,45,87]\\nprint(exp_list1, type(exp_list1))\\nexp_list1.sort()\\nprint(exp_list1, type(exp_list1))',\n",
       "  'exp_list1 = [3,6,2,7,12,42,45,87]\\nprint(exp_list1, type(exp_list1))\\nexp_list1.sort()\\nprint(exp_list1, type(exp_list1))',\n",
       "  \"def sort_fun(lst):\\n    '''\\n    sort by second element in one input list\\n    '''\\n    return lst[1]\\n\\nexp_list1 = [[3,5],[6,17],[2,23],[7,0],[12,3],[42,41],[45,33],[87,100]]\\nprint(exp_list1, type(exp_list1))\\nexp_list1.sort(key=sort_fun)\\nprint(exp_list1, type(exp_list1))\",\n",
       "  'import bisect\\n\\nbisect_list = [3,23,6,4,3,6,7,12,34,52,5,0,12]\\nbisect_list.sort()\\nres = bisect.bisect(bisect_list,8)\\nprint(res)\\nbisect.insort(bisect_list,8)\\nbisect_list',\n",
       "  'import bisect\\n\\nbisect_list = [3,23,6,4,3,60,7,15,34,52,5,0,12]\\nbisect_list.sort()\\nres = bisect.bisect(bisect_list,8)\\nprint(res)\\nbisect.insort(bisect_list,8)\\nbisect_list',\n",
       "  'import bisect\\n\\nbisect_list = [13,23,6,4,3,60,7,15,34,52,5,0,12]\\nbisect_list.sort()\\nres = bisect.bisect(bisect_list,8)\\nprint(res)\\nbisect.insort(bisect_list,8)\\nbisect_list',\n",
       "  'import bisect\\n\\nbisect_list = [13,23,6,4,3,60,7,15,34,52,5,0,12]\\nbisect_list.sort()\\nres = bisect.bisect(bisect_list,8)\\nprint(res)\\nbisect.insort(bisect_list,8)\\nbisect_list',\n",
       "  '# transfer list into dict\\nexp_dict = {}\\nexp_list = [\"foo\",\\'bar\\',\\'tar\\',\\'war\\',\\'mar\\']\\nfor i,v in enumerate(exp_list):\\n    exp_dict[v] = i\\nexp_dict',\n",
       "  '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nexp_deict2',\n",
       "  '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nexp_dict2',\n",
       "  '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nexp_dict2\\nprint(type(zip(exp_seq1,exp_seq2)))',\n",
       "  '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nprint(exp_dict2)\\nprint(type(zip(exp_seq1,exp_seq2)))\\nprint(zip(exp_seq1,exp_seq2))',\n",
       "  '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nprint(exp_dict2)\\nprint(type(zip(exp_seq1,exp_seq2)))\\nprint(zip(exp_seq1,exp_seq2))\\nprint(list(zip(exp_seq1,exp_seq2)))',\n",
       "  '# zip used for unzip\\ncoach = [(\"Charlie\",\"Brown\"),\\n         (\"Lucy\",\"Amuderson\"),\\n         (\"Lisa\",\"Mocca\"),\\n         (\"Peppermint\",\"Party\"),\\n         (\"Marcy\",\"Green\")]\\nfirst_name, last_name = zip(*coach)\\nfirst_name',\n",
       "  '# zip used for unzip\\n# zip here just combine together all first names and last names\\ncoach = [(\"Charlie\",\"Brown\"),\\n         (\"Lucy\",\"Amuderson\"),\\n         (\"Lisa\",\"Mocca\"),\\n         (\"Peppermint\",\"Party\"),\\n         (\"Marcy\",\"Green\")]\\nfirst_name, last_name = zip(*coach)\\nfirst_name',\n",
       "  '# create a dict from tuple\\nexp_dict3 = dict(zip(range(5),reversed(range(5,10))))',\n",
       "  '# create a dict from tuple\\nexp_dict3 = dict(zip(range(5),reversed(range(5,10))))\\nexp_dict3',\n",
       "  '# insert new value into dict\\nfrom collections import defaultdict\\n\\nwords = {\"apple\",\"apricot\",\"banana\",\"banjiu\",\"citrus\",\"camellia\",\"brown tea\"}\\nby_letter = defaultd0ct(list)\\nfor word in words:\\n    by_letter[word[0]].append(word)\\nby_letter',\n",
       "  '# insert new value into dict\\nfrom collections import defaultdict\\n\\nwords = {\"apple\",\"apricot\",\"banana\",\"banjiu\",\"citrus\",\"camellia\",\"brown tea\"}\\nby_letter = defaultdict(list)\\nfor word in words:\\n    by_letter[word[0]].append(word)\\nby_letter',\n",
       "  '# list comprehension\\nstrings = [\"apply\", \\'comply\\', \\'imply\\', \\'dupply\\', \\'ply\\', \\'fly\\', \\'ly\\']\\nres_strings = [x.upper() for x in strings if len(x) >=2 and len(x) < 5]\\nres_strings',\n",
       "  '# list comprehension\\nstrings = [\"apply\", \\'comply\\', \\'imply\\', \\'dupply\\', \\'ply\\', \\'fly\\', \\'ly\\']\\nres_strings = [x.upper() for x in strings if len(x) >=2 and len(x) < 6]\\nres_strings',\n",
       "  '# set comprehension\\nres_length = {len(x) for x in strings}\\nres_length',\n",
       "  '# dict comprehesion',\n",
       "  '# dict comprehension',\n",
       "  '# set comprehension\\nres_length = {len(x) for x in strings} #本质上就是一个map函数\\nres_length2 = set(map(len, strings))\\nprint(res_length)\\nres_length2',\n",
       "  '# dict comprehension\\nres_dict = {idx: val for ids,val in enumerate(strings)}\\nres_dict',\n",
       "  '# dict comprehension\\nres_dict = {idx: val for idx,val in enumerate(strings)}\\nres_dict',\n",
       "  '# nested list|tup comprehension',\n",
       "  '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if name.contains(T)]',\n",
       "  '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if name.contain(T,t)]',\n",
       "  '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if name.str.contains(T,t)]',\n",
       "  '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if name.contains(T,t)]',\n",
       "  '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if \"T\" not in name]',\n",
       "  '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if \"T\" not in name]\\nflatten_list',\n",
       "  '# transfer list to tuple\\nres_tuple = [(name for name in names) for names in nested_list]\\nres_tuple',\n",
       "  '# transfer list to tuple\\nres_tuple = [(list(name for name in names)) for names in nested_list]\\nres_tuple',\n",
       "  '# transfer list to tuple\\nres_tuple = [(tup(name for name in names)) for names in nested_list]\\nres_tuple',\n",
       "  '# transfer list to tuple\\nres_tuple = [(tuple(name for name in names)) for names in nested_list]\\nres_tuple',\n",
       "  'local()',\n",
       "  'global()',\n",
       "  'global',\n",
       "  'globals()',\n",
       "  'locals()'],\n",
       " '_oh': {25: [3, 4, 5, 6, 7],\n",
       "  26: 7,\n",
       "  27: 7,\n",
       "  28: [1, 'this is changed', 5, 3.6, 'foo', [3, 5]],\n",
       "  29: [1, 'this is changed', 5, 3.6, 'foo', [3, 5]],\n",
       "  34: [0, 3, 3, 4, 5, 6, 6, 7, 8, 12, 12, 23, 34, 52],\n",
       "  35: [0, 3, 3, 4, 5, 6, 7, 8, 12, 15, 23, 34, 52, 60],\n",
       "  36: [0, 3, 4, 5, 6, 7, 8, 12, 13, 15, 23, 34, 52, 60],\n",
       "  37: [0, 3, 4, 5, 6, 7, 8, 12, 13, 15, 23, 34, 52, 60],\n",
       "  38: {'foo': 0, 'bar': 1, 'tar': 2, 'war': 3, 'mar': 4},\n",
       "  40: {0: ('foo', 'A'), 1: ('bar', 'B'), 2: ('tar', 'C'), 3: ('war', 'D')},\n",
       "  44: ('Charlie', 'Lucy', 'Lisa', 'Peppermint', 'Marcy'),\n",
       "  45: ('Charlie', 'Lucy', 'Lisa', 'Peppermint', 'Marcy'),\n",
       "  47: {0: 9, 1: 8, 2: 7, 3: 6, 4: 5},\n",
       "  49: defaultdict(list,\n",
       "              {'c': ['camellia', 'citrus'],\n",
       "               'a': ['apple', 'apricot'],\n",
       "               'b': ['banana', 'banjiu', 'brown tea']}),\n",
       "  50: ['PLY', 'FLY', 'LY'],\n",
       "  51: ['APPLY', 'IMPLY', 'PLY', 'FLY', 'LY'],\n",
       "  52: {2, 3, 5, 6},\n",
       "  55: {2, 3, 5, 6},\n",
       "  57: {0: 'apply',\n",
       "   1: 'comply',\n",
       "   2: 'imply',\n",
       "   3: 'dupply',\n",
       "   4: 'ply',\n",
       "   5: 'fly',\n",
       "   6: 'ly'},\n",
       "  64: ['Mary', 'David', 'Jake', 'Lucy'],\n",
       "  65: [<generator object <listcomp>.<genexpr> at 0x7fb1a3b89d60>,\n",
       "   <generator object <listcomp>.<genexpr> at 0x7fb1a3b89eb0>],\n",
       "  66: [['Mary', 'Tom', 'David', 'Thomas'], ['Tim', 'Tong', 'Jake', 'Lucy']],\n",
       "  68: [('Mary', 'Tom', 'David', 'Thomas'), ('Tim', 'Tong', 'Jake', 'Lucy')],\n",
       "  72: {...}},\n",
       " '_dh': ['/Users/collinsliu/jupyter/python_learning_basics/python for data analysis'],\n",
       " 'In': ['',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,instance_c,(str,float))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,instance_c,(str,float)))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance((instance_b,instance_c),(str,float)))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = 4\\ninstance_c = 5.5\\nprint(isinstance((instance_b,instance_c),(int,float)))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = 4\\ninstance_c = 5.5\\nprint(isinstance((instance_b,instance_c),(float,float)))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = 4\\ninstance_c = 5.5\\nprint(isinstance((instance_b,int)))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = 4\\ninstance_c = 5.5\\nprint(isinstance(instance_b,int))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,str))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,str))\\nprint(isinstance(instance_c,(int, long ,float)))',\n",
       "  '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,str))\\nprint(isinstance(instance_c,(int, float)))',\n",
       "  '# decide whether the object has __iter__ function - which means it\\'s iterrable\\ninstance_d = \"a string\"\\nprint(isiterable(instance_d))',\n",
       "  'print(\"{:.2f} is float\".format(instance_c))',\n",
       "  '# print formatted resulat\\nprint(\"{:.2f} is float\".format(instance_c))',\n",
       "  '# print formatted results\\nprint(\"{:.2f} is float\".format(instance_c))',\n",
       "  '# print formatted results\\nprint(\"{:.2f} is float\".format(instance_c))',\n",
       "  'from datetime import date, time, datetime',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.month)',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.time)',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)\\nprint(dt.strftime(\"%Y%m%d %h:%m:%s\"))',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)\\nprint(dt.strftime(\"%Y-%m-%d %h:%m:%s\"))',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)\\nprint(dt.strftime(\"%Y-%m-%d %h:%m:%s\"))\\nprint(datetime.strptime(\"20221109194838\",\"%Y-%m-%d %h:%m:%s\"))',\n",
       "  'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)\\nprint(dt.strftime(\"%Y-%m-%d %h:%m:%s\"))\\nprint(datetime.strptime(\"20221109\",\"%Y-%m-%d\"))',\n",
       "  '# tuple unpacking\\nexp_list = [(1,2,3), (4,5,6), (7,8,9)]\\nfor a,b,c in exp_list:\\n    print(\"a={0}, b={1}, c={2}\".format(a,b,c))',\n",
       "  '# tuple discarding\\nexp_tup = 1,2,3,4,5,6,7\\nprint(exp_tup)\\na,b,*rest = exp_tup\\nrest',\n",
       "  '# tuple common method\\nexp_tup2 = 1,2,3,4,54,3,3,3,3,3,3\\nexp_tup2.count(3)',\n",
       "  '# tuple common method - built-in methods for tuple\\nexp_tup2 = 1,2,3,4,54,3,3,3,3,3,3\\nexp_tup2.count(3)',\n",
       "  '# list could be modified in-place\\nexp_list = [1, 3, 5, 3.6, \"foo\", [3,5]]\\nexp_list',\n",
       "  'exp_list[1] = \"this is changed\"\\nexp_list',\n",
       "  'exp_list1 = [3,6,2,7,12,42,45,87]\\nprint(exp_list1, type(exp_list1))\\nexp_list1.sort()\\nprint(exp_list1, type(exp_list1))',\n",
       "  'exp_list1 = [3,6,2,7,12,42,45,87]\\nprint(exp_list1, type(exp_list1))\\nexp_list1.sort()\\nprint(exp_list1, type(exp_list1))',\n",
       "  'exp_list1 = [3,6,2,7,12,42,45,87]\\nprint(exp_list1, type(exp_list1))\\nexp_list1.sort()\\nprint(exp_list1, type(exp_list1))',\n",
       "  \"def sort_fun(lst):\\n    '''\\n    sort by second element in one input list\\n    '''\\n    return lst[1]\\n\\nexp_list1 = [[3,5],[6,17],[2,23],[7,0],[12,3],[42,41],[45,33],[87,100]]\\nprint(exp_list1, type(exp_list1))\\nexp_list1.sort(key=sort_fun)\\nprint(exp_list1, type(exp_list1))\",\n",
       "  'import bisect\\n\\nbisect_list = [3,23,6,4,3,6,7,12,34,52,5,0,12]\\nbisect_list.sort()\\nres = bisect.bisect(bisect_list,8)\\nprint(res)\\nbisect.insort(bisect_list,8)\\nbisect_list',\n",
       "  'import bisect\\n\\nbisect_list = [3,23,6,4,3,60,7,15,34,52,5,0,12]\\nbisect_list.sort()\\nres = bisect.bisect(bisect_list,8)\\nprint(res)\\nbisect.insort(bisect_list,8)\\nbisect_list',\n",
       "  'import bisect\\n\\nbisect_list = [13,23,6,4,3,60,7,15,34,52,5,0,12]\\nbisect_list.sort()\\nres = bisect.bisect(bisect_list,8)\\nprint(res)\\nbisect.insort(bisect_list,8)\\nbisect_list',\n",
       "  'import bisect\\n\\nbisect_list = [13,23,6,4,3,60,7,15,34,52,5,0,12]\\nbisect_list.sort()\\nres = bisect.bisect(bisect_list,8)\\nprint(res)\\nbisect.insort(bisect_list,8)\\nbisect_list',\n",
       "  '# transfer list into dict\\nexp_dict = {}\\nexp_list = [\"foo\",\\'bar\\',\\'tar\\',\\'war\\',\\'mar\\']\\nfor i,v in enumerate(exp_list):\\n    exp_dict[v] = i\\nexp_dict',\n",
       "  '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nexp_deict2',\n",
       "  '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nexp_dict2',\n",
       "  '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nexp_dict2\\nprint(type(zip(exp_seq1,exp_seq2)))',\n",
       "  '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nprint(exp_dict2)\\nprint(type(zip(exp_seq1,exp_seq2)))\\nprint(zip(exp_seq1,exp_seq2))',\n",
       "  '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nprint(exp_dict2)\\nprint(type(zip(exp_seq1,exp_seq2)))\\nprint(zip(exp_seq1,exp_seq2))\\nprint(list(zip(exp_seq1,exp_seq2)))',\n",
       "  '# zip used for unzip\\ncoach = [(\"Charlie\",\"Brown\"),\\n         (\"Lucy\",\"Amuderson\"),\\n         (\"Lisa\",\"Mocca\"),\\n         (\"Peppermint\",\"Party\"),\\n         (\"Marcy\",\"Green\")]\\nfirst_name, last_name = zip(*coach)\\nfirst_name',\n",
       "  '# zip used for unzip\\n# zip here just combine together all first names and last names\\ncoach = [(\"Charlie\",\"Brown\"),\\n         (\"Lucy\",\"Amuderson\"),\\n         (\"Lisa\",\"Mocca\"),\\n         (\"Peppermint\",\"Party\"),\\n         (\"Marcy\",\"Green\")]\\nfirst_name, last_name = zip(*coach)\\nfirst_name',\n",
       "  '# create a dict from tuple\\nexp_dict3 = dict(zip(range(5),reversed(range(5,10))))',\n",
       "  '# create a dict from tuple\\nexp_dict3 = dict(zip(range(5),reversed(range(5,10))))\\nexp_dict3',\n",
       "  '# insert new value into dict\\nfrom collections import defaultdict\\n\\nwords = {\"apple\",\"apricot\",\"banana\",\"banjiu\",\"citrus\",\"camellia\",\"brown tea\"}\\nby_letter = defaultd0ct(list)\\nfor word in words:\\n    by_letter[word[0]].append(word)\\nby_letter',\n",
       "  '# insert new value into dict\\nfrom collections import defaultdict\\n\\nwords = {\"apple\",\"apricot\",\"banana\",\"banjiu\",\"citrus\",\"camellia\",\"brown tea\"}\\nby_letter = defaultdict(list)\\nfor word in words:\\n    by_letter[word[0]].append(word)\\nby_letter',\n",
       "  '# list comprehension\\nstrings = [\"apply\", \\'comply\\', \\'imply\\', \\'dupply\\', \\'ply\\', \\'fly\\', \\'ly\\']\\nres_strings = [x.upper() for x in strings if len(x) >=2 and len(x) < 5]\\nres_strings',\n",
       "  '# list comprehension\\nstrings = [\"apply\", \\'comply\\', \\'imply\\', \\'dupply\\', \\'ply\\', \\'fly\\', \\'ly\\']\\nres_strings = [x.upper() for x in strings if len(x) >=2 and len(x) < 6]\\nres_strings',\n",
       "  '# set comprehension\\nres_length = {len(x) for x in strings}\\nres_length',\n",
       "  '# dict comprehesion',\n",
       "  '# dict comprehension',\n",
       "  '# set comprehension\\nres_length = {len(x) for x in strings} #本质上就是一个map函数\\nres_length2 = set(map(len, strings))\\nprint(res_length)\\nres_length2',\n",
       "  '# dict comprehension\\nres_dict = {idx: val for ids,val in enumerate(strings)}\\nres_dict',\n",
       "  '# dict comprehension\\nres_dict = {idx: val for idx,val in enumerate(strings)}\\nres_dict',\n",
       "  '# nested list|tup comprehension',\n",
       "  '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if name.contains(T)]',\n",
       "  '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if name.contain(T,t)]',\n",
       "  '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if name.str.contains(T,t)]',\n",
       "  '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if name.contains(T,t)]',\n",
       "  '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if \"T\" not in name]',\n",
       "  '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if \"T\" not in name]\\nflatten_list',\n",
       "  '# transfer list to tuple\\nres_tuple = [(name for name in names) for names in nested_list]\\nres_tuple',\n",
       "  '# transfer list to tuple\\nres_tuple = [(list(name for name in names)) for names in nested_list]\\nres_tuple',\n",
       "  '# transfer list to tuple\\nres_tuple = [(tup(name for name in names)) for names in nested_list]\\nres_tuple',\n",
       "  '# transfer list to tuple\\nres_tuple = [(tuple(name for name in names)) for names in nested_list]\\nres_tuple',\n",
       "  'local()',\n",
       "  'global()',\n",
       "  'global',\n",
       "  'globals()',\n",
       "  'locals()'],\n",
       " 'Out': {25: [3, 4, 5, 6, 7],\n",
       "  26: 7,\n",
       "  27: 7,\n",
       "  28: [1, 'this is changed', 5, 3.6, 'foo', [3, 5]],\n",
       "  29: [1, 'this is changed', 5, 3.6, 'foo', [3, 5]],\n",
       "  34: [0, 3, 3, 4, 5, 6, 6, 7, 8, 12, 12, 23, 34, 52],\n",
       "  35: [0, 3, 3, 4, 5, 6, 7, 8, 12, 15, 23, 34, 52, 60],\n",
       "  36: [0, 3, 4, 5, 6, 7, 8, 12, 13, 15, 23, 34, 52, 60],\n",
       "  37: [0, 3, 4, 5, 6, 7, 8, 12, 13, 15, 23, 34, 52, 60],\n",
       "  38: {'foo': 0, 'bar': 1, 'tar': 2, 'war': 3, 'mar': 4},\n",
       "  40: {0: ('foo', 'A'), 1: ('bar', 'B'), 2: ('tar', 'C'), 3: ('war', 'D')},\n",
       "  44: ('Charlie', 'Lucy', 'Lisa', 'Peppermint', 'Marcy'),\n",
       "  45: ('Charlie', 'Lucy', 'Lisa', 'Peppermint', 'Marcy'),\n",
       "  47: {0: 9, 1: 8, 2: 7, 3: 6, 4: 5},\n",
       "  49: defaultdict(list,\n",
       "              {'c': ['camellia', 'citrus'],\n",
       "               'a': ['apple', 'apricot'],\n",
       "               'b': ['banana', 'banjiu', 'brown tea']}),\n",
       "  50: ['PLY', 'FLY', 'LY'],\n",
       "  51: ['APPLY', 'IMPLY', 'PLY', 'FLY', 'LY'],\n",
       "  52: {2, 3, 5, 6},\n",
       "  55: {2, 3, 5, 6},\n",
       "  57: {0: 'apply',\n",
       "   1: 'comply',\n",
       "   2: 'imply',\n",
       "   3: 'dupply',\n",
       "   4: 'ply',\n",
       "   5: 'fly',\n",
       "   6: 'ly'},\n",
       "  64: ['Mary', 'David', 'Jake', 'Lucy'],\n",
       "  65: [<generator object <listcomp>.<genexpr> at 0x7fb1a3b89d60>,\n",
       "   <generator object <listcomp>.<genexpr> at 0x7fb1a3b89eb0>],\n",
       "  66: [['Mary', 'Tom', 'David', 'Thomas'], ['Tim', 'Tong', 'Jake', 'Lucy']],\n",
       "  68: [('Mary', 'Tom', 'David', 'Thomas'), ('Tim', 'Tong', 'Jake', 'Lucy')],\n",
       "  72: {...}},\n",
       " 'get_ipython': <bound method InteractiveShell.get_ipython of <ipykernel.zmqshell.ZMQInteractiveShell object at 0x7fb1a192f040>>,\n",
       " 'exit': <IPython.core.autocall.ZMQExitAutocall at 0x7fb1a19f0250>,\n",
       " 'quit': <IPython.core.autocall.ZMQExitAutocall at 0x7fb1a19f0250>,\n",
       " '_': {...},\n",
       " '__': [('Mary', 'Tom', 'David', 'Thomas'), ('Tim', 'Tong', 'Jake', 'Lucy')],\n",
       " '___': [['Mary', 'Tom', 'David', 'Thomas'], ['Tim', 'Tong', 'Jake', 'Lucy']],\n",
       " '_i': 'globals()',\n",
       " '_ii': 'global',\n",
       " '_iii': 'global()',\n",
       " '_i1': '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,instance_c,(str,float))',\n",
       " '_i2': '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,instance_c,(str,float)))',\n",
       " 'instance_a': 5,\n",
       " 'instance_b': 'name',\n",
       " 'instance_c': 5.5,\n",
       " '_i3': '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance((instance_b,instance_c),(str,float)))',\n",
       " '_i4': '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = 4\\ninstance_c = 5.5\\nprint(isinstance((instance_b,instance_c),(int,float)))',\n",
       " '_i5': '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = 4\\ninstance_c = 5.5\\nprint(isinstance((instance_b,instance_c),(float,float)))',\n",
       " '_i6': '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = 4\\ninstance_c = 5.5\\nprint(isinstance((instance_b,int)))',\n",
       " '_i7': '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = 4\\ninstance_c = 5.5\\nprint(isinstance(instance_b,int))',\n",
       " '_i8': '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,str))',\n",
       " '_i9': '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,str))\\nprint(isinstance(instance_c,(int, long ,float)))',\n",
       " '_i10': '# decide whether the object is of one kind - isinstance\\ninstance_a = 5\\nprint(isinstance(instance_a, int))\\nprint(\"######\")\\ninstance_b = \"name\"\\ninstance_c = 5.5\\nprint(isinstance(instance_b,str))\\nprint(isinstance(instance_c,(int, float)))',\n",
       " '_i11': '# decide whether the object has __iter__ function - which means it\\'s iterrable\\ninstance_d = \"a string\"\\nprint(isiterable(instance_d))',\n",
       " 'instance_d': 'a string',\n",
       " '_i12': 'print(\"{:.2f} is float\".format(instance_c))',\n",
       " '_i13': '# print formatted resulat\\nprint(\"{:.2f} is float\".format(instance_c))',\n",
       " '_i14': '# print formatted results\\nprint(\"{:.2f} is float\".format(instance_c))',\n",
       " '_i15': '# print formatted results\\nprint(\"{:.2f} is float\".format(instance_c))',\n",
       " '_i16': 'from datetime import date, time, datetime',\n",
       " 'date': datetime.date,\n",
       " 'time': datetime.time,\n",
       " 'datetime': datetime.datetime,\n",
       " '_i17': 'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.month)',\n",
       " 'dt': datetime.datetime(2022, 11, 9, 19, 46, 50),\n",
       " '_i18': 'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.time)',\n",
       " '_i19': 'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)',\n",
       " '_i20': 'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)\\nprint(dt.strftime(\"%Y%m%d %h:%m:%s\"))',\n",
       " '_i21': 'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)\\nprint(dt.strftime(\"%Y-%m-%d %h:%m:%s\"))',\n",
       " '_i22': 'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)\\nprint(dt.strftime(\"%Y-%m-%d %h:%m:%s\"))\\nprint(datetime.strptime(\"20221109194838\",\"%Y-%m-%d %h:%m:%s\"))',\n",
       " '_i23': 'from datetime import date, time, datetime\\n\\ndt = datetime(2022,11,9,19,46,50)\\nprint(dt.hour)\\nprint(dt.strftime(\"%Y-%m-%d %h:%m:%s\"))\\nprint(datetime.strptime(\"20221109\",\"%Y-%m-%d\"))',\n",
       " '_i24': '# tuple unpacking\\nexp_list = [(1,2,3), (4,5,6), (7,8,9)]\\nfor a,b,c in exp_list:\\n    print(\"a={0}, b={1}, c={2}\".format(a,b,c))',\n",
       " 'exp_list': ['foo', 'bar', 'tar', 'war', 'mar'],\n",
       " 'a': 1,\n",
       " 'b': 2,\n",
       " 'c': 9,\n",
       " '_i25': '# tuple discarding\\nexp_tup = 1,2,3,4,5,6,7\\nprint(exp_tup)\\na,b,*rest = exp_tup\\nrest',\n",
       " 'exp_tup': (1, 2, 3, 4, 5, 6, 7),\n",
       " 'rest': [3, 4, 5, 6, 7],\n",
       " '_25': [3, 4, 5, 6, 7],\n",
       " '_i26': '# tuple common method\\nexp_tup2 = 1,2,3,4,54,3,3,3,3,3,3\\nexp_tup2.count(3)',\n",
       " 'exp_tup2': (1, 2, 3, 4, 54, 3, 3, 3, 3, 3, 3),\n",
       " '_26': 7,\n",
       " '_i27': '# tuple common method - built-in methods for tuple\\nexp_tup2 = 1,2,3,4,54,3,3,3,3,3,3\\nexp_tup2.count(3)',\n",
       " '_27': 7,\n",
       " '_i28': '# list could be modified in-place\\nexp_list = [1, 3, 5, 3.6, \"foo\", [3,5]]\\nexp_list',\n",
       " '_28': [1, 'this is changed', 5, 3.6, 'foo', [3, 5]],\n",
       " '_i29': 'exp_list[1] = \"this is changed\"\\nexp_list',\n",
       " '_29': [1, 'this is changed', 5, 3.6, 'foo', [3, 5]],\n",
       " '_i30': 'exp_list1 = [3,6,2,7,12,42,45,87]\\nprint(exp_list1, type(exp_list1))\\nexp_list1.sort()\\nprint(exp_list1, type(exp_list1))',\n",
       " 'exp_list1': [[7, 0],\n",
       "  [12, 3],\n",
       "  [3, 5],\n",
       "  [6, 17],\n",
       "  [2, 23],\n",
       "  [45, 33],\n",
       "  [42, 41],\n",
       "  [87, 100]],\n",
       " '_i31': 'exp_list1 = [3,6,2,7,12,42,45,87]\\nprint(exp_list1, type(exp_list1))\\nexp_list1.sort()\\nprint(exp_list1, type(exp_list1))',\n",
       " '_i32': 'exp_list1 = [3,6,2,7,12,42,45,87]\\nprint(exp_list1, type(exp_list1))\\nexp_list1.sort()\\nprint(exp_list1, type(exp_list1))',\n",
       " '_i33': \"def sort_fun(lst):\\n    '''\\n    sort by second element in one input list\\n    '''\\n    return lst[1]\\n\\nexp_list1 = [[3,5],[6,17],[2,23],[7,0],[12,3],[42,41],[45,33],[87,100]]\\nprint(exp_list1, type(exp_list1))\\nexp_list1.sort(key=sort_fun)\\nprint(exp_list1, type(exp_list1))\",\n",
       " 'sort_fun': <function __main__.sort_fun(lst)>,\n",
       " '_i34': 'import bisect\\n\\nbisect_list = [3,23,6,4,3,6,7,12,34,52,5,0,12]\\nbisect_list.sort()\\nres = bisect.bisect(bisect_list,8)\\nprint(res)\\nbisect.insort(bisect_list,8)\\nbisect_list',\n",
       " 'bisect': <module 'bisect' from '/Users/collinsliu/opt/anaconda3/lib/python3.9/bisect.py'>,\n",
       " 'bisect_list': [0, 3, 4, 5, 6, 7, 8, 12, 13, 15, 23, 34, 52, 60],\n",
       " 'res': 6,\n",
       " '_34': [0, 3, 3, 4, 5, 6, 6, 7, 8, 12, 12, 23, 34, 52],\n",
       " '_i35': 'import bisect\\n\\nbisect_list = [3,23,6,4,3,60,7,15,34,52,5,0,12]\\nbisect_list.sort()\\nres = bisect.bisect(bisect_list,8)\\nprint(res)\\nbisect.insort(bisect_list,8)\\nbisect_list',\n",
       " '_35': [0, 3, 3, 4, 5, 6, 7, 8, 12, 15, 23, 34, 52, 60],\n",
       " '_i36': 'import bisect\\n\\nbisect_list = [13,23,6,4,3,60,7,15,34,52,5,0,12]\\nbisect_list.sort()\\nres = bisect.bisect(bisect_list,8)\\nprint(res)\\nbisect.insort(bisect_list,8)\\nbisect_list',\n",
       " '_36': [0, 3, 4, 5, 6, 7, 8, 12, 13, 15, 23, 34, 52, 60],\n",
       " '_i37': 'import bisect\\n\\nbisect_list = [13,23,6,4,3,60,7,15,34,52,5,0,12]\\nbisect_list.sort()\\nres = bisect.bisect(bisect_list,8)\\nprint(res)\\nbisect.insort(bisect_list,8)\\nbisect_list',\n",
       " '_37': [0, 3, 4, 5, 6, 7, 8, 12, 13, 15, 23, 34, 52, 60],\n",
       " '_i38': '# transfer list into dict\\nexp_dict = {}\\nexp_list = [\"foo\",\\'bar\\',\\'tar\\',\\'war\\',\\'mar\\']\\nfor i,v in enumerate(exp_list):\\n    exp_dict[v] = i\\nexp_dict',\n",
       " 'exp_dict': {'foo': 0, 'bar': 1, 'tar': 2, 'war': 3, 'mar': 4},\n",
       " 'i': 3,\n",
       " 'v': ('war', 'D'),\n",
       " '_38': {'foo': 0, 'bar': 1, 'tar': 2, 'war': 3, 'mar': 4},\n",
       " '_i39': '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nexp_deict2',\n",
       " 'exp_seq1': ('foo', 'bar', 'tar', 'war'),\n",
       " 'exp_seq2': ('A', 'B', 'C', 'D'),\n",
       " 'exp_dict2': {0: ('foo', 'A'),\n",
       "  1: ('bar', 'B'),\n",
       "  2: ('tar', 'C'),\n",
       "  3: ('war', 'D')},\n",
       " '_i40': '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nexp_dict2',\n",
       " '_40': {0: ('foo', 'A'), 1: ('bar', 'B'), 2: ('tar', 'C'), 3: ('war', 'D')},\n",
       " '_i41': '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nexp_dict2\\nprint(type(zip(exp_seq1,exp_seq2)))',\n",
       " '_i42': '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nprint(exp_dict2)\\nprint(type(zip(exp_seq1,exp_seq2)))\\nprint(zip(exp_seq1,exp_seq2))',\n",
       " '_i43': '# zip common usage example\\nexp_seq1 = (\"foo\",\"bar\",\"tar\",\"war\")\\nexp_seq2 = (\"A\",\"B\",\"C\",\"D\")\\nexp_dict2 = {}\\nfor i,v in enumerate(zip(exp_seq1, exp_seq2)):\\n    exp_dict2[i] = v\\nprint(exp_dict2)\\nprint(type(zip(exp_seq1,exp_seq2)))\\nprint(zip(exp_seq1,exp_seq2))\\nprint(list(zip(exp_seq1,exp_seq2)))',\n",
       " '_i44': '# zip used for unzip\\ncoach = [(\"Charlie\",\"Brown\"),\\n         (\"Lucy\",\"Amuderson\"),\\n         (\"Lisa\",\"Mocca\"),\\n         (\"Peppermint\",\"Party\"),\\n         (\"Marcy\",\"Green\")]\\nfirst_name, last_name = zip(*coach)\\nfirst_name',\n",
       " 'coach': [('Charlie', 'Brown'),\n",
       "  ('Lucy', 'Amuderson'),\n",
       "  ('Lisa', 'Mocca'),\n",
       "  ('Peppermint', 'Party'),\n",
       "  ('Marcy', 'Green')],\n",
       " 'first_name': ('Charlie', 'Lucy', 'Lisa', 'Peppermint', 'Marcy'),\n",
       " 'last_name': ('Brown', 'Amuderson', 'Mocca', 'Party', 'Green'),\n",
       " '_44': ('Charlie', 'Lucy', 'Lisa', 'Peppermint', 'Marcy'),\n",
       " '_i45': '# zip used for unzip\\n# zip here just combine together all first names and last names\\ncoach = [(\"Charlie\",\"Brown\"),\\n         (\"Lucy\",\"Amuderson\"),\\n         (\"Lisa\",\"Mocca\"),\\n         (\"Peppermint\",\"Party\"),\\n         (\"Marcy\",\"Green\")]\\nfirst_name, last_name = zip(*coach)\\nfirst_name',\n",
       " '_45': ('Charlie', 'Lucy', 'Lisa', 'Peppermint', 'Marcy'),\n",
       " '_i46': '# create a dict from tuple\\nexp_dict3 = dict(zip(range(5),reversed(range(5,10))))',\n",
       " 'exp_dict3': {0: 9, 1: 8, 2: 7, 3: 6, 4: 5},\n",
       " '_i47': '# create a dict from tuple\\nexp_dict3 = dict(zip(range(5),reversed(range(5,10))))\\nexp_dict3',\n",
       " '_47': {0: 9, 1: 8, 2: 7, 3: 6, 4: 5},\n",
       " '_i48': '# insert new value into dict\\nfrom collections import defaultdict\\n\\nwords = {\"apple\",\"apricot\",\"banana\",\"banjiu\",\"citrus\",\"camellia\",\"brown tea\"}\\nby_letter = defaultd0ct(list)\\nfor word in words:\\n    by_letter[word[0]].append(word)\\nby_letter',\n",
       " 'defaultdict': collections.defaultdict,\n",
       " 'words': {'apple',\n",
       "  'apricot',\n",
       "  'banana',\n",
       "  'banjiu',\n",
       "  'brown tea',\n",
       "  'camellia',\n",
       "  'citrus'},\n",
       " '_i49': '# insert new value into dict\\nfrom collections import defaultdict\\n\\nwords = {\"apple\",\"apricot\",\"banana\",\"banjiu\",\"citrus\",\"camellia\",\"brown tea\"}\\nby_letter = defaultdict(list)\\nfor word in words:\\n    by_letter[word[0]].append(word)\\nby_letter',\n",
       " 'by_letter': defaultdict(list,\n",
       "             {'c': ['camellia', 'citrus'],\n",
       "              'a': ['apple', 'apricot'],\n",
       "              'b': ['banana', 'banjiu', 'brown tea']}),\n",
       " 'word': 'apricot',\n",
       " '_49': defaultdict(list,\n",
       "             {'c': ['camellia', 'citrus'],\n",
       "              'a': ['apple', 'apricot'],\n",
       "              'b': ['banana', 'banjiu', 'brown tea']}),\n",
       " '_i50': '# list comprehension\\nstrings = [\"apply\", \\'comply\\', \\'imply\\', \\'dupply\\', \\'ply\\', \\'fly\\', \\'ly\\']\\nres_strings = [x.upper() for x in strings if len(x) >=2 and len(x) < 5]\\nres_strings',\n",
       " 'strings': ['apply', 'comply', 'imply', 'dupply', 'ply', 'fly', 'ly'],\n",
       " 'res_strings': ['APPLY', 'IMPLY', 'PLY', 'FLY', 'LY'],\n",
       " '_50': ['PLY', 'FLY', 'LY'],\n",
       " '_i51': '# list comprehension\\nstrings = [\"apply\", \\'comply\\', \\'imply\\', \\'dupply\\', \\'ply\\', \\'fly\\', \\'ly\\']\\nres_strings = [x.upper() for x in strings if len(x) >=2 and len(x) < 6]\\nres_strings',\n",
       " '_51': ['APPLY', 'IMPLY', 'PLY', 'FLY', 'LY'],\n",
       " '_i52': '# set comprehension\\nres_length = {len(x) for x in strings}\\nres_length',\n",
       " 'res_length': {2, 3, 5, 6},\n",
       " '_52': {2, 3, 5, 6},\n",
       " '_i53': '# dict comprehesion',\n",
       " '_i54': '# dict comprehension',\n",
       " '_i55': '# set comprehension\\nres_length = {len(x) for x in strings} #本质上就是一个map函数\\nres_length2 = set(map(len, strings))\\nprint(res_length)\\nres_length2',\n",
       " 'res_length2': {2, 3, 5, 6},\n",
       " '_55': {2, 3, 5, 6},\n",
       " '_i56': '# dict comprehension\\nres_dict = {idx: val for ids,val in enumerate(strings)}\\nres_dict',\n",
       " '_i57': '# dict comprehension\\nres_dict = {idx: val for idx,val in enumerate(strings)}\\nres_dict',\n",
       " 'res_dict': {0: 'apply',\n",
       "  1: 'comply',\n",
       "  2: 'imply',\n",
       "  3: 'dupply',\n",
       "  4: 'ply',\n",
       "  5: 'fly',\n",
       "  6: 'ly'},\n",
       " '_57': {0: 'apply',\n",
       "  1: 'comply',\n",
       "  2: 'imply',\n",
       "  3: 'dupply',\n",
       "  4: 'ply',\n",
       "  5: 'fly',\n",
       "  6: 'ly'},\n",
       " '_i58': '# nested list|tup comprehension',\n",
       " '_i59': '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if name.contains(T)]',\n",
       " 'nested_list': [['Mary', 'Tom', 'David', 'Thomas'],\n",
       "  ['Tim', 'Tong', 'Jake', 'Lucy']],\n",
       " '_i60': '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if name.contain(T,t)]',\n",
       " '_i61': '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if name.str.contains(T,t)]',\n",
       " '_i62': '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if name.contains(T,t)]',\n",
       " '_i63': '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if \"T\" not in name]',\n",
       " 'flatten_list': ['Mary', 'David', 'Jake', 'Lucy'],\n",
       " '_i64': '# nested list|tup comprehension\\nnested_list = [[\"Mary\",\"Tom\",\"David\",\"Thomas\"],\\n               [\"Tim\",\"Tong\",\"Jake\",\"Lucy\"]]\\n# flatten the list\\nflatten_list = [name for names in nested_list for name in names if \"T\" not in name]\\nflatten_list',\n",
       " '_64': ['Mary', 'David', 'Jake', 'Lucy'],\n",
       " '_i65': '# transfer list to tuple\\nres_tuple = [(name for name in names) for names in nested_list]\\nres_tuple',\n",
       " 'res_tuple': [('Mary', 'Tom', 'David', 'Thomas'),\n",
       "  ('Tim', 'Tong', 'Jake', 'Lucy')],\n",
       " '_65': [<generator object <listcomp>.<genexpr> at 0x7fb1a3b89d60>,\n",
       "  <generator object <listcomp>.<genexpr> at 0x7fb1a3b89eb0>],\n",
       " '_i66': '# transfer list to tuple\\nres_tuple = [(list(name for name in names)) for names in nested_list]\\nres_tuple',\n",
       " '_66': [['Mary', 'Tom', 'David', 'Thomas'], ['Tim', 'Tong', 'Jake', 'Lucy']],\n",
       " '_i67': '# transfer list to tuple\\nres_tuple = [(tup(name for name in names)) for names in nested_list]\\nres_tuple',\n",
       " '_i68': '# transfer list to tuple\\nres_tuple = [(tuple(name for name in names)) for names in nested_list]\\nres_tuple',\n",
       " '_68': [('Mary', 'Tom', 'David', 'Thomas'), ('Tim', 'Tong', 'Jake', 'Lucy')],\n",
       " '_i69': 'local()',\n",
       " '_i70': 'global()',\n",
       " '_i71': 'global',\n",
       " '_i72': 'globals()',\n",
       " '_72': {...},\n",
       " '_i73': 'locals()'}"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "locals()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "856161af",
   "metadata": {},
   "source": [
    "### namespace"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4dc76485",
   "metadata": {},
   "source": [
    "## functions\n",
    "1. the returns of a function is default tuple\n",
    "2. return of a function could also be set to dictionary\n",
    "3. function  coulc be treated as obejcts\n",
    "4. anonymous function - lambda function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "ac45ac8c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 10, 'b': 20, 'c': 30}"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def func():\n",
    "    a = 10\n",
    "    b = 20\n",
    "    c = 30\n",
    "    return {\"a\": a, \"b\":b, \"c\":c}\n",
    "func()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "99bcbc5b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Beijing', 'Nanjing', 'ShangHai', 'LANZHOU', 'g uang z hou', 'CHongQing']"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "# function would be treated as object, which could be passed in tuples\n",
    "cities = [\"Beijing\",\"Nan?jing\",\"Shang##Hai\",\"LANZHOU\",\"g uang z hou\",\"CHon!gQ!@#ing\"]\n",
    "\n",
    "def remove_punctuation(value):\n",
    "    return re.sub(\"[?#!@]\",\"\",value)\n",
    "\n",
    "\n",
    "partial_res_city = map(remove_punctuation, cities)\n",
    "list(partial_res_city)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "6273eefb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Beijing', 'Nanjing', 'Shanghai', 'Lanzhou', 'G Uang Z Hou', 'Chongqing']"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "functions = [str.strip, remove_punctuation, str.title]\n",
    "\n",
    "def clean_string(strings, ops):\n",
    "    res = []\n",
    "    for string in strings:\n",
    "        for function in ops:\n",
    "            string = function(string)\n",
    "        res.append(string)\n",
    "    return res\n",
    "\n",
    "res_city = clean_string(cities,functions)\n",
    "res_city"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a24431d4",
   "metadata": {},
   "source": [
    "#### lambda function - another way for function simplification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "46e47dde",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 9, 25, 4, 36, 16, 144]"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "# suppose for a simple apply function\n",
    "def apply_to_list(obj, f):\n",
    "    return [f(x) for x in obj]\n",
    "\n",
    "some_list = [1,3,5,2,6,4,12]\n",
    "res = apply_to_list(some_list, lambda x : int(math.pow(x,2)))\n",
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "e283c916",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['aaaa', 'foo', 'barbarbar', 'banana', 'aaabbaadddd', 'cartar', 'boycat']"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# also lambda function could be used for sort\n",
    "some_strings = [\"foo\", \"barbarbar\", \"cartar\", \"banana\", \"boycat\", \"aaaa\", \"aaabbaadddd\"]\n",
    "res = sorted(some_strings, key=lambda x: len(set(list(x))) )\n",
    "res"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00c9d3e8",
   "metadata": {},
   "source": [
    "#### Currying - one method for partial function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "8948c93e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from functools import partial\n",
    "\n",
    "def apply_add(x,y):\n",
    "    return x + y\n",
    "\n",
    "apply_add_five = partial(apply_add, 5)\n",
    "res = apply_add_five(4)\n",
    "res"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65087bf9",
   "metadata": {},
   "source": [
    "### iterator\n",
    "1. iter() function will generate one iterator "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "cc5af013",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<list_iterator at 0x7fb1a3b28d30>"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "one_exp_list = [1.4, 3.0, 5.7, 12.23, 6.01]\n",
    "iter(one_exp_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e289f2ad",
   "metadata": {},
   "source": [
    "#### itertools - built-in func package designed for iterable objects\n",
    "1. combinations(iterable, k)\n",
    "2. permutations(iterable, k)\n",
    "3. groupby(iterable, keyfunc)\n",
    "4. product(\\*iterables, repeat=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "043759da",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A ['Amy']\n",
      "C ['Charlie', 'Candy']\n",
      "M ['Mark', 'Mosses']\n",
      "E ['Ezikel', 'Emma']\n",
      "D ['David', 'Dunky', 'Dunken']\n"
     ]
    }
   ],
   "source": [
    "import itertools\n",
    "\n",
    "some_names = [\"Amy\", \"Charlie\", \"Candy\", \"Mark\", \"Mosses\", \"Ezikel\", \"Emma\", \"David\", \"Dunky\", \"Dunken\"]\n",
    "for idx, val in itertools.groupby(some_names, lambda x: x[0]):\n",
    "    print(idx,list(val))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9410fdbb",
   "metadata": {},
   "source": [
    "# numpy - import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5edecd8e",
   "metadata": {},
   "source": [
    "## built-in functions and properties\n",
    "1. ndim dtype shape \n",
    "2. function: full, full_like, eye, identity, zeros_like, ones_like, array|asarray, arange\n",
    "3. astype() \n",
    "4. one note: all operations of slicing take place on the original ndarray, otherwise a **copy()** function is needed\n",
    "5. <font color=red>for 3d array(most common), the order for slicing should be **height-row-column**</font>\n",
    "6. boolean function applied on numpy array will always return a boolean array the same structure of the original ndarray; **make sure that all boolean arithmatic operations only have one symbol, that is | & !**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "0a94a9cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6.304347826086957\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[[ 5.        ,  5.2173913 ,  5.43478261,  5.65217391],\n",
       "        [ 5.86956522,  6.08695652,  6.30434783,  6.52173913]],\n",
       "\n",
       "       [[ 6.73913043,  6.95652174,  7.17391304,  7.39130435],\n",
       "        [ 7.60869565,  7.82608696,  8.04347826,  8.26086957]],\n",
       "\n",
       "       [[ 8.47826087,  8.69565217,  8.91304348,  9.13043478],\n",
       "        [ 9.34782609,  9.56521739,  9.7826087 , 10.        ]]])"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "some_arr = np.linspace(5,10,24).reshape(3,2,4)\n",
    "print(some_arr[0,1,2])\n",
    "some_arr"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0075b890",
   "metadata": {},
   "source": [
    "### booolean function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "372be3b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.01379675  1.26788776 -0.10144308 -1.94386531  0.10892521  0.64140899]\n",
      " [ 0.39238022 -0.60525769  0.30474783 -0.01709643  0.47631887 -2.14930589]\n",
      " [-0.17004812 -0.42554714 -0.20412542 -0.82259763 -0.4300066  -1.12688866]\n",
      " [-0.24979829 -0.12506382  0.5875104   1.3267701   0.91762253 -1.3391061 ]\n",
      " [ 0.96514331  2.25468126  0.58462851  1.44611972 -0.74452604 -0.67770117]\n",
      " [-0.51675793  1.00320723  0.04805853  1.08730085  0.9186774   1.76182643]\n",
      " [ 1.12399156 -1.399       1.28184586 -0.88414047 -0.33409872 -0.36424341]]\n"
     ]
    }
   ],
   "source": [
    "names = np.array([\"Bob\", \"Lucy\", \"Mary\", \"Bob\", \"Thomas\", \"Will\", \"Mary\"])\n",
    "values = np.random.randn(7,6)\n",
    "print(values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "be0a82ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True, False, False,  True, False,  True, False])"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mask = (names == \"Bob\") | (names == \"Will\")\n",
    "mask"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "557dca11",
   "metadata": {},
   "source": [
    "### scalar operation\n",
    "1. just applied to all elements in ndarray\n",
    "2. change in values will always replace the original value\n",
    "3. data slicing including **column-based slicing** and **boolean function slicing**\n",
    "4. <font color=red>all boolean function will always return one boolean array</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "a84d98c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.43294344 -0.55423116 -0.15564094  0.61531015  0.04607718]\n",
      " [ 0.33427187  0.81298562  1.42346451 -0.39177768 -0.18494286]\n",
      " [ 1.11136169 -0.74552395  1.79718555 -0.99087628 -0.94369027]\n",
      " [ 0.26862531 -0.41797091  0.20812654  0.76546257  0.1641475 ]]\n",
      "[[ 0.43294344 -0.55423116 -0.15564094  0.          0.04607718]\n",
      " [ 0.33427187  0.          0.         -0.39177768 -0.18494286]\n",
      " [ 0.         -0.74552395  0.         -0.99087628 -0.94369027]\n",
      " [ 0.26862531 -0.41797091  0.20812654  0.          0.1641475 ]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "arr = np.random.randn(4,5)\n",
    "print(arr)\n",
    "arr[arr>0.5] = 0\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3a5a8e74",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
